【问题标题】:Entity Framework 4.3 CF many - to - many relationship saving object?Entity Framework 4.3 CF 多对多关系保存对象?
【发布时间】:2012-05-15 19:02:31
【问题描述】:

当使用 EF 4.3 代码优先方法创建多对多关系时,我无法将数据保存到连接表,也无法提供有关如何使用将对象保存到 Icollection 来填充此表的任何示例...这是我的示例:

模型

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; }
}

控制器

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 = "johndoe@gmail.com";
    o.Hospitals.Add(h);

    db.SaveChanges();
 }

使用这种方法,即使我的 Hospital 实例充满了数据,我也会在这里不断出错:o.Hospitals.Add(h);。如何将数据保存到两个表中,dbo.Operatorsdbo.OperatorHospital 是关系表?

【问题讨论】:

    标签: entity-framework many-to-many entity-framework-4.3 savechanges


    【解决方案1】:

    o.Hospitals.Add(h) 将失败,因为该列表是一个空列表。您不能在空列表上调用 Add()。通常,大多数人通过在实体的构造函数中实例化列表来解决这个问题......就像这样......由于 CSharp 问题,当前行正在爆炸。

    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>();
       }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-18
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多