Search code examples
entity-frameworkmany-to-manyentity-framework-4.3savechanges

Entity Framework 4.3 CF many - to - many relationship saving object?


When creating many to many relationship using EF 4.3 code first approach, I cannot save data to connecting table, also cannot any examples on how to fill this table using saving object to Icollection... Here is my example:

MODELS

public class Hospital
{
    //PK
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public Guid User_Id { get; set; }

    //FK
    public virtual ICollection<Operator> Operators { get; set; }

}

public class Operator
{
    //PK
    [Key]
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Dob { get; set; }
    public string Email { get; set; }

    //FK
    public virtual ICollection<Hospital> Hospitals { get; set; }
}

public class Project: DbContext
{
    public DbSet<Hospital> Hospitals { get; set; }
    public DbSet<Operator> Operators { get; set; }
}

CONTROLLER

public void AddOperater()
{

    Hospital h = new Hospital();
    h = db.Hospitals.Single(a=>a.Id ==1);

    var o = new Operator();
    o.FirstName = "John";
    o.LastName = "Doe";
    o.Dob = new DateTime(1988,2,12);
    o.Email = "[email protected]";
    o.Hospitals.Add(h);

    db.SaveChanges();
 }

With this approach I keep getting error here: o.Hospitals.Add(h); even when my Hospital instance is filled with data. How exactly to save data to both tables, the dbo.Operators and dbo.OperatorHospital which is relationship table?


Solution

  • o.Hospitals.Add(h) will fail because the list is a null list. You cannot call Add() on a null list. Typically most people get around this by instantiating the list in the constructor of the entity... like so... the current line is blowing up due to a CSharp issue.

    public class Hospital
    {
        //PK
        [Key]
        public int Id { get; set; }
    
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public Guid User_Id { get; set; }
    
         //FK
        public virtual ICollection<Operator> Operators { get; set; }
    
        public Hospital()
        {
             Operators = new List<Operator>();
        }
    
    }
    
    
    public class Operator
    {
       //PK
       [Key]
       public int Id { get; set; }
    
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public DateTime Dob { get; set; }
       public string Email { get; set; }
    
       //FK
       public virtual ICollection<Hospital> Hospitals { get; set; }
    
       public Operator()
       {
           Hospitals = new List<Hospital>();
       }
    

    }