C#Entity Framework,使用多对多连接向数据库添加数据

时间:2017-12-23 17:43:43

标签: c# entity-framework many-to-many

我对编程很陌生,对数据库也很陌生。我使用CodeFirst方法创建了一个本地数据库。 Database model

Work类的代码是:

public class Work
    {
        public int WorkId{ get; set; }
        public string Name { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public string Position { get; set; }
        public int SalaryMinimum { get; set; }
        public int SalaryMaximum { get; set; }
        public string Education { get; set; }
        public int ClientID { get; set; }
        public virtual List<WorkCondition> WorkCondition { get; set; }
    }

WorkCondition类的代码是:

 public class WorkCondition
    {
        public int WorkConditionId { get; set; }
        public string Name { get; set; }
        public string Description{ get; set; }
        public virtual List<Work> Work { get; set; }
    }

DbContext代码,它构成了多对多关系:

 public class CrmDbContext : DbContext
    {
        public CrmDbContext() : base("name=CrmSQL") { }
        public DbSet<Client> Client { get; set; }
        public DbSet<Work> Work { get; set; }
        public DbSet<WorkCondition> WorkCondition {get; set;}
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Work>()
                        .HasMany<WorkCondition>(wc => wc.WorkCondition)
                        .WithMany(w => w.Work)
                        .Map(wwc =>
                        {
                            wwc.MapLeftKey("WorkId");
                            wwc.MapRightKey("WorkConditionId");
                            wwc.ToTable("WorkWorkCondition");
                        });

        }
    }

&#34; enable-migartion&#34;,&#34; add-migration Initial&#34;和&#34;更新数据库&#34; EF制作了一个表WorkWorkCondition来管理多对多关系,有两列:WorkConditionId和WorkId。

到目前为止,我添加新工作的代码如下所示:

using (var db = new CrmDbContext())
{
    var work1 = new Work
    {
        Name = "Tech Play add 2",
        Start = DateTime.Now,
        End = DateTime.Now,
        SalaryMinimum = 2000,
        SalaryMaximum = 3000,
        WorkCondition = new List<WorkCondition>(),
        ClientID = 2
    };
    //WorkConditionId = 1 and WorkConditionId = 3 have to be associated with 
    //this new work 
    db.Work.Add(work1);
    db.SaveChanges();
}

所以,我的问题是我无法理解如何插入数据,因此EF会自动填充WorkWorkCondition表。在我的情况下,我想添加具有多个WorkConditions的新工作。 WorkConditions填充了3行,我想通过WorkConditionId引用它们。

1 个答案:

答案 0 :(得分:0)

结束了这段代码,绝对不是最好的代码,但它对我有用。

using (var db = new CrmDbContext())
{
    List<WorkCondition> wc2 = new List<WorkCondition>();
    wc2.Add(db.WorkCondition.Where(wcid => wcid.WorkConditionId == 3).SingleOrDefault());
    wc2.Add(db.WorkCondition.Where(wcid => wcid.WorkConditionId == 1).SingleOrDefault());

    var work1 = new Work
    {
        Name = "Tech Play add 2",
        Start = DateTime.Now,
        End = DateTime.Now,
        SalaryMinimum = 2000,
        SalaryMaximum = 3000,
        WorkCondition = wc2
    };
    db.Work.Add(work1);
    db.SaveChanges();
}