【问题标题】:Joining entities together将实体连接在一起
【发布时间】:2015-02-04 14:00:58
【问题描述】:

我的数据库有以下两个实体 -

public class App
{
    public int AppID { get; set; }
    public string Business { get; set; }
    public string ApName { get; set; }
    public string FirstContact { get; set; }

    public virtual Colleague Colleague { get; set; }
}


public class Colleague
{
    public int ColleagueID { get; set; }
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public string EmailAddress { get; set; }
    public int PhoneNumber { get; set; }

    public virtual ICollection<App> App { get; set; }
}

同事实体可以有多个不同的应用程序,因此是集合,但我不知道如何将表实际链接在一起。

目前我正在播种我的实体,如下所示,但我如何修改此播种数据以显示同事应与哪个应用关联?

        var colleagues  = new List<Colleague>
        {
            new Colleague{FirstName="test", SecondName="test", EmailAddress="test@test.hotmail.com", PhoneNumber=000000},
            new Colleague{FirstName="test2", SecondName="test2", EmailAddress="test@test.hotmail.com", PhoneNumber=000000},
            new Colleague{FirstName="test3", SecondName="test3", EmailAddress="test@test.hotmail.com", PhoneNumber=000000},
        };

        colleagues.ForEach(s => context.Colleagues.Add(s));

        var apps = new List<App>
        {
        new App{ApName="app1",Business="business"},
        new App{ApName="app2",Business="business"},
        new App{ApName="app3",Business="business"},
        new App{ApName="app4",Business="business"},
        new App{ApName="app5",Business="business"},
        new App{ApName="app6",Business="business"},
        new App{ApName="app7",Business="business"},
        };
        apps.ForEach(s => context.Apps.Add(s));
        context.SaveChanges();

【问题讨论】:

    标签: entity-framework model-view-controller


    【解决方案1】:

    使用该模型,您正在创建一对多关系,因此 EF 将在您的 Apps 表中为您创建一个 ColleageId FK。

    现在,您有多种选择来更新您的数据。让我们从Colleague 开始。我的第一个建议是添加一个构造函数来初始化您的集合:

    public Colleague()
    {
       Apps=new HashSet<App>();// Could be a List<> too
    }
    

    现在,假设您想同时添加一个新的Colleague 和一个新的App,并且您想将两者关联起来。你的代码应该是这样的:

     var newColleague=new Colleague{FirstName="test4", SecondName="test4", EmailAddress="test4@test.hotmail.com", PhoneNumber=000000};
     newColleage.Add( new App{ApName="app7",Business="business"});
     context.Add(newColleague);
     context.SaveChanges();
    

    EF 会保存这两个实体,你不需要在其DBSet 中添加新的App

    第二种情况是,如果您想在 DB 中的 Colleague 中添加新的 App

    var colleage=context.FirstOrDefault(c=>c.EmailAddress=="test@test.hotmail.com");
    colleage.Apps.Add(new App{ApName="app8",Business="business"});
    context.SaveChanges();
    

    第三种情况是如果两者都在数据库中。在这种情况下,您必须找到 then 稍后才能关联它们:

    var colleage=context.FirstOrDefault(c=>c.EmailAddress=="test@test.hotmail.com");
    var app=context.FirstOrDefault(a=>a.ApName=="app1");
    colleague.Add(app);
    context.SaveChanges();
    

    现在在App的情况下,要修改它与Colleague的关系,你可以这样做:

    var colleage=context.FirstOrDefault(c=>c.EmailAddress=="test@test.hotmail.com");
    var app=context.FirstOrDefault(a=>a.ApName=="app1");
    app.Colleague=colleague;
    context.SaveChanges();
    

    顺便说一句,当您想向DBSet 添加多个元素时,可以使用AddRange 方法:

    context.Colleagues.AddRange(colleagues);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      相关资源
      最近更新 更多