我上定义的从属或主两个实体之间的关系是什么? [英] Do I define a relationship between two entities on the dependent or the principal?

查看:153
本文介绍了我上定义的从属或主两个实体之间的关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用实体框架,我感到困惑的地方,我需要定义两个实体之间的关系。我觉得每当我找实例,我结束了从两个不同的角度寻找同样的事情的例子 - 依赖型RARR;校长,以及主要与RARR;依赖性



由于以下实体:

 类Foo 
{
公众诠释标识{搞定;组; }
公共虚拟的ICollection<酒吧GT;酒吧{搞定;组; }
}

类酒吧
{
公众诠释标识{搞定;组; }
公共富富{搞定;组; }
公共虚拟巴兹{搞定;组; }
}

类巴兹
{
公众诠释标识{搞定;组; }
公共酒吧酒吧{搞定;组; }
}

我们这里有几种方案。 有许多酒吧指点下吧。 巴兹有一个可选的外键酒吧巴兹无需酒吧存在指定。



会在哪里我定义这些关系?到哪里,我用流利的API,它的实体将这些关系相对于被定义为当是什么意思?也许,使其更清晰,如果我用流利的API和 EntityTypeConfiguration 的绑定类,为此实体将这些吗?


$定义b $ b

为什么我感到困惑的一个例子是,因为我看到答案如这个是说一个一到应该与虚拟的类定义。因此,在这些实体中,可选一到一间巴兹酒吧会或类似的东西:

  modelBuilder.Entity<酒吧>()
.HasOptional(F => f.Baz)
.WithRequired(S => s.Bar);


解决方案

我觉得你是根据<$ C $困惑C>虚拟关键字。我也试图找到它,你链接到页面上,但它不存在。



虚拟关键字可以实体框架覆盖,在其创建的幕后,其中一个代理实体属性从酒吧继承。然后,覆盖将使数据库调用延迟加载巴兹当访问该属性。



虚拟关键字无关的关系的定义,如果你不想偷懒。加载,你不需要它。



的定义当你映射了校长:

  modelBuilder.Entity<酒吧和GT;()
.HasOptional(F => f.Baz)。 //巴兹依赖
.WithRequired(S = GT; s.Bar); // Bar是本金

modelBuilder.Entity<酒吧及GT;()
.HasOptional(F = GT; f.Bar)。 // Bar是依赖
.WithRequired(S = GT; s.Baz); //巴兹是本金

至于在你的榜样之间的酒吧其他关系美孚酒吧集合,但只有一个酒吧这样的外键的推移酒吧。 EF会做,默认情况下。



的依赖得到引用的主要重点外键。当它是一对一的,即外键都是依赖的主键,但EF不能制定出哪个是哪个,这就是为什么直到你指定,你会得到一个错误。



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


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.

Given the following entities:

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

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.

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?

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

解决方案

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.

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.

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

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.

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.

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

这篇关于我上定义的从属或主两个实体之间的关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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