代码优先:独立协会与外国关联协会? [英] Code First: Independent associations vs. Foreign key associations?

查看:113
本文介绍了代码优先:独立协会与外国关联协会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我开始研究一个新项目,并且我正在设计我的POCO时,我都会和自己进行精神辩论。我看到很多教程/代码示例似乎赞成外键关联

I have a mental debate with myself every time I start working on a new project and I am designing my POCOs. I have seen many tutorials/code samples that seem to favor foreign key associations:

public class Order
{
    public int ID { get; set; }
    public int CustomerID { get; set; } // <-- Customer ID
    ...
}

public class Order
{
    public int ID { get; set; }
    public Customer Customer { get; set; } // <-- Customer object
    ...
}

过去我已经与NHibernate合作过,并且使用了独立的协会,这不仅仅是感觉到更多的OO,而且(以懒惰的方式加载)具有让我访问整个Customer对象的优点,而不仅仅是它的ID。这样我就可以检索一个Order实例,然后执行 Order.Customer.FirstName ,而不必明确地做一个连接,这是非常方便的。

I have worked with NHibernate in the past, and used independent associations, which not only feel more OO, but also (with lazy loading) have the advantage of giving me access to the whole Customer object, instead of just its ID. This allows me to, for example, retrieve an Order instance and then do Order.Customer.FirstName without having to do a join explicitly, which is extremely convenient.

所以要回顾一下,我的问题是:

So to recap, my questions are:


  1. $ b $有什么重大缺点b使用独立关联?和...

  2. 如果没有,什么
    将是使用外键关联的原因?


推荐答案

如果要充分利用ORM,您一定会使用实体参考:

If you want to take full advantage of ORM you will definitely use Entity reference:

public class Order
{
    public int ID { get; set; }
    public Customer Customer { get; set; } // <-- Customer object
    ...
}

一旦从具有FK的数据库生成实体模型,它将始终生成实体引用。如果不想使用它们,则必须手动修改EDMX文件并添加表示FK的属性。至少在Entity Framework v1中,只允许使用独立关联。

Once you generate an entity model from a database with FKs it will always generate entity references. If you don't want to use them you must manually modify the EDMX file and add properties representing FKs. At least this was the case in Entity Framework v1 where only Independent associations were allowed.

实体框架v4提供了一种称为外键关联的新类型的关联。独立和外键协会之间最明显的区别是在Order类中:

Entity framework v4 offers a new type of association called Foreign key association. The most obvious difference between the independent and the foreign key association is in Order class:

public class Order
{
    public int ID { get; set; }
    public int CustomerId { get; set; }  // <-- Customer ID
    public Customer Customer { get; set; } // <-- Customer object
    ...
}

您可以看到您同时拥有FK属性和实体引用。两种类型的关联之间有更多的区别:

As you can see you have both FK property and entity reference. There are more differences between two types of associations:

独立关联


  • 它在 ObjectStateManager 中表示为单独的对象。它有自己的 EntityState

  • 当建立关联时,您始终需要来自两端的权限

  • 此协会的映射方式与实体相同。

  • It is represented as separate object in ObjectStateManager. It has its own EntityState!
  • When building association you always need entitites from both ends of association
  • This association is mapped in the same way as entity.

外键关联


  • 它不被表示为 ObjectStateManager 中的单独对象。由于您必须遵循一些特殊规则。

  • 建立关联时,不需要关联的两端。具有子实体和父实体的PK,但PK值必须是唯一的。因此,当使用外键关联时,您还必须为关系中使用的新生成的实体分配临时唯一ID。

  • 此关联未映射,而是定义参照约束。

  • It is not represented as separate object in ObjectStateManager. Due to that you must follow some special rules.
  • When building association you don't need both ends of association. It is enough to have child entity and PK of parent entity but PK value must be unique. So when using foreign keys association you must also assign temporary unique IDs to newly generated entities used in relations.
  • This association is not mapped but instead it defines referential constraints.

如果要使用外键关联,您必须在实体数据模型向导中勾选在模型中包含外键列。

If you want to use foreign key association you must tick Include foreign key columns in the model in Entity Data Model Wizard.

编辑:

我发现这两种类型的关联之间的区别不是很清楚,所以我写了一篇短文,涵盖了这个有更多的细节和我对此的看法。

I found that the difference between these two types of associations is not very well known so I wrote a short article covering this with more details and my own opinion about this.

这篇关于代码优先:独立协会与外国关联协会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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