实体框架代码首先初始化外键 [英] Entity Framework Code First initializing foreign keys

查看:125
本文介绍了实体框架代码首先初始化外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码的样子:

public class Family
{
    public int FamilyID { get; set; }
    public virtual ICollection<Member> FamilyMembers { get; set; }
}
public class Member
{
    public int MemberID { get; set; }
    public virtual Family Family { get; set; }
}
public class Bicycle
{
    public int BicycleID { get; set; }
    public virtual Member Owner { get; set; }
}
public class MyContext : DbContext
{
    public MyContext : base () { }
}

因此,成员与自行车之间存在一对一关系,家庭与成员之间存在一对多关系. 我的问题是:如何使用上下文初始化这些外键?互联网上的所有教程都介绍了如何定义,但现在介绍了如何初始化它们.我是简单地为他们分配一个正确的int值,还是给他们分配一个完整的对象,如果是,我该怎么做?使用LINQ吗?

So, there is a one-to-one relantionship between Member and Bicycle, and there is one-to-many relationship between Family and Member. My question is: how do I initialize these foreign keys using my context? All the tutorials on the internet say how to define, but now how to initialize them. Do I simply assign them a correct int value or do I assign them a whole object, and if so, how do I do that? Using LINQ?

@ 假设有2个家庭:史密斯(Smith)和约翰逊(Johnson).史密斯有3名成员:本,艾米,布莱恩.约翰逊有2名成员:约翰和娜塔莉.如何在代码中初始化所有这些内容?

@ Assume there are 2 families: Smith and Johnson. Smith has 3 members: Ben, Amy, Brian. Johnson has 2 members: John and Natalie. How do I initialize all that in my code?

推荐答案

首先,解决此问题:

public class Member
{
    public int MemberID { get; set; }
    public virtual Family Family { get; set; }
}

这足以配置关系. EF将按惯例确定密钥的身份.

That should be enough to configure the relationships. EF will make the keys identity by convention.

您可以通过多种方式分配关系.如果将FK ID添加到模型中,则可以显式设置FK ID,也可以使用如下导航属性:

You can assign the relationships multiple ways. You can explicitly set FK Ids if you add them to your models, or you can use the navigation properties like this:

var family = new Family { LastName = "Smith", ...};
List<Member> members = new List<Member>
{
    new Member(){ Name = "Ben", ... },
    new Member(){ Name = "Amy", ... },
    new Member(){ Name = "Brian", ... }
};
family.FamilyMembers = members;
context.Family.Add(family);
context.SaveChanges();

要将自行车分配给所有者,请执行以下操作:

To assign a bike to an owner:

var owner = context.Members.Find(ownerId);
var bike = new Bicycle
{
    Make = "ACME",
    Model = "XXXX",
    Owner = owner
};
context.Bicycles.Add(bike);
context.SaveChanges();

是的,当然可以将FK添加到模型中.那就是我使用的方法.像这样的代码:

Yes, it is certainly permissible to add FK to your model. That's the method I use. Like this code:

public class Member
{
    public int MemberID { get; set; }
    public int FamilyID { get; set; }  // EF will automatically make this a FK by convention
    public virtual Family Family { get; set; }
}

如果您不遵守约定或出于文档目的而希望使用它们,则需要添加注释:

You need to add annotations if you don't adhere to convention or if you want them for documentation sake:

public class Member
{
    public int MemberID { get; set; }
    public int FamilyID { get; set; }
    [ForeignKey("FamilyID")]
    public virtual Family Family { get; set; }
}

请参见 http://www.entityframeworktutorial .net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

我更喜欢流利的api.它可以使我的模型保持整洁并分离关注点.对于简单的项目,您可以在上下文中的OnModelCreating()覆盖中添加所有流畅的代码,但是我更喜欢将实体配置存储在上下文下的文件夹中(每个实体一个文件),如下所示:

I prefer the fluent api. It keeps my models cleaner and separates concerns. For simple projects you can add all the fluent code in the OnModelCreating() override in your context, but I prefer to store my entity configurations in a folder under my context (one file per entity) as described here: http://odetocode.com/blogs/scott/archive/2011/11/28/composing-entity-framework-fluent-configurations.aspx

实际上,您可以使EF使用此处描述的技术自动找到您的流利代码: https://msdn.microsoft.com/zh-CN/magazine/dn519921.aspx

You can actually have EF automatically find your fluent code using the technique described here: https://msdn.microsoft.com/en-us/magazine/dn519921.aspx

关于子集合,是的,您可以在构造函数中对其进行更新:

Regarding the child collections, yes, you can new it up in the constructor:

public class Family
{
    public Family()
    {
        FamilyMembers = new HashSet<Member>();
    }

    public int FamilyID { get; set; }
    public virtual ICollection<Member> FamilyMembers { get; set; }
}

这篇关于实体框架代码首先初始化外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆