Code First:独立关联与外键关联? [英] Code First: Independent associations vs. Foreign key associations?

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

问题描述

每次我开始从事一个新项目并设计我的 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. 是否有任何明显的缺点使用独立协会?和...
  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.

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

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