我是否定义了依赖者或委托人之间的两个实体之间的关系? [英] Do I define a relationship between two entities on the dependent or the principal?

查看:73
本文介绍了我是否定义了依赖者或委托人之间的两个实体之间的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架时,我需要定义两个实体之间的关系,我感到困惑。我觉得,每当我寻找例子,我最终从两个不同的角度找到相同的事情的例子 - 依赖和rarr;校长以及校长

When using Entity Framework, I get confused as to where I need to define a relationship between two entities. I feel like whenever I look for examples, I end up finding examples of the same thing from two different perspectives - dependent → principal, as well as principal → dependent.

给定以下实体:

class Foo
{
    public int Id { get; set; }
    public virtual ICollection<Bar> Bars { get; set; }
}

class Bar
{
    public int Id { get; set; }
    public Foo Foo { get; set; }
    public virtual Baz { get; set; }
}

class Baz
{
    public int Id { get; set; }
    public Bar Bar { get; set; }
}

这里有几个场景。 Foo 有很多 Bar 指向它。 Baz 具有 Bar 的可选外键。 Baz 可以不指定 Bar

We have several scenarios here. Foo has many Bars pointing to it. Baz has an optional foreign key to Bar. Baz can exist without Bar specified.

我定义了这些关系?在哪里,我的意思是使用流畅的API,哪些实体将这些关系定义为?也许让它更清楚,如果我使用流畅的API和 EntityTypeConfiguration 类绑定,这些定义在哪个实体?

Where would I defined these relations? By where, I mean when using fluent API, which entity would these relations be defined in relation to? Maybe to make it more clear, if I were using fluent API and EntityTypeConfiguration classes for binding, for which entity would these be defined in?

为什么我感到困惑的一个例子是因为我看到像这个人这样的答案应该在课程中使用 virtual 定义一对一。因此,在这些实体中, Baz Bar 之间的可选一对一将是或类似于以下内容:

An example of why I'm confused is because I see answers like this one that say that a one-to-one should be defined in the class with the virtual. So in these entities, the optional one-to-one between Baz and Bar would be, or something similar to:

modelBuilder.Entity<bar>()
            .HasOptional(f => f.Baz)
            .WithRequired(s => s.Bar);


推荐答案

我认为你被 virtual 关键字。我确实尝试在您链接到的页面上找到它,但它不在。

I think you are confused by the virtual keyword. I did try to find it on the page you linked to, but it's not there.

virtual 关键字实体框架覆盖了它在幕后创建的代理实体中的属性,并从 Bar 继承。然后,当访问该属性时,覆盖将使数据库调用延迟加载 Baz

The virtual keyword lets the Entity Framework override that property in a proxy entity that it creates behind the scenes and which inherits from Bar. The override will then make a database call to lazy load Baz when the property is accessed.

virtual 关键字与关系的定义无关,如果你不想懒惰

The virtual keyword has nothing to do with the definition of the relationship, and if you don't want lazy loading, you don't need it.

在您映射时定义主体:

You define the principal when you map:

modelBuilder.Entity<bar>()
            .HasOptional(f => f.Baz). //Baz is dependent
            .WithRequired(s => s.Bar);//Bar is principal

modelBuilder.Entity<bar>()
            .HasOptional(f => f.Bar). //Bar is dependent
            .WithRequired(s => s.Baz);//Baz is principal

至于您在 Foo Bar 之间的另一个关系, Foo 有一个集合 Bars Foo 只有一个 Bar ,所以外键继续 Bar 。 EF默认情况下会这样做。

As for the other relationship in your example between Foo and Bar, Foo has a collection of Bars but Foo has only one Bar so the foreign key goes on Bar. EF will do that by default.

依赖者获取引用主体键的外键。当它是一对一的时候,这个外键也是依赖的主键,但EF无法解决哪个是哪一个,这就是为什么在你指定之前就会出错。

The dependent gets the foreign key that references the principal's key. When it's a one to one, that foreign key is also the dependent's primary key but EF can't work out which is which and that's why you get an error until you've specified it.

参考: http: //msdn.microsoft.com/en-us/library/ee382827.aspx

这篇关于我是否定义了依赖者或委托人之间的两个实体之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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