什么是独立的协会和外键关联? [英] What are Independent Associations and Foreign Key Associations?

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

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/5281974/$c$c-first-independent-associations-vs-foreign-key-associations\">$c$c第一:独立协会与外键关联

在EF 4或5 EF code一,什么是独立协会,什么是外键关联,所用的在MSDN 或<一个href=\"http://stackoverflow.com/questions/11110802/foreign-key-vs-independent-relationships-is-there-improvement-with-entity-fra\">Foreign重点vs.独立关系 - 是有改进的实体框架5 (强调):

In EF 4 or EF 5 Code First, what is an "independent association" and what is a "foreign key association", as used on MSDN or Foreign Key vs. Independent Relationships - is there improvement with Entity Framework 5? (emphasis added):

2.4.1使用外键关联,以减少视图发电成本

2.4.1 Using Foreign Key Associations to reduce view generation cost

我们已经看到了一些情况下,如果开关的关联
  从模型的独立协会外键关联
  显着改善的时间花在视图生成。

We have seen a number of cases where switching the associations in the model from Independent Associations to Foreign Key Associations dramatically improved the time spent in view generation.

所以 - 我现在知道用哪个。如果只有我知道他们是什么,以及如何人会转换为它!我的问题的话,是,你会如何定义这些条款?什么流利/注解/惯例相互调用?

So -- now I know which to use. If only I knew what they were and how one would convert to it! My question, then, is, how would you define those terms? What fluent/annotations/conventions invoke each?

推荐答案

外键关联是你除了相应的导航属性在模型中的外键属性。独立协会是当你在你的数据库中有一个外键列,但对应于该列的外键属性是不是在你的模型 - 即你有一个NavigationProperty但没有外键的属性,会告诉你什么样的ID值相关的性质是不实际去相关的属性。

Foreign Key Association is where you have a foreign key property in your model in addition to the corresponding Navigation Property. Independent Association is when you have a foreign key column in your database but the foreign key property corresponding to this column is not in your model - i.e. you have a NavigationProperty but there is no foreign key property that would tell you what the ID value of the related property is without actually going to the related property.

下面是一个独立协会(注意从属没有一个外键 - 只是导航属性)的模型的例子:

Here is an example of a model with an Independent Association (notice that the Dependent does not have a foreign key - just navigation property):

public class Dependent
{
    public int Id { get; set; }

    [Required]
    public Principal PrincipalEntity { get; set; }

}

public class Principal
{
    public int Id { get; set; }
    public ICollection<Dependent> DependentEntities { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Dependent> Dependents { get; set; }
    public DbSet<Principal> Principals { get; set; }
}

这是同一型号的,但随着ForeignKey的协会(注意PrincipalEntity_Id属性和[ForeignKey的()]属性)的例子:

And here is an example of the same model but with the ForeignKey Association (notice the PrincipalEntity_Id property and the [ForeignKey()] attribute):

public class Dependent
{
    public int Id { get; set; }

    public int PrincipalEntity_Id { get; set; }

    [Required]
    [ForeignKey("PrincipalEntity_Id")]
    public Principal PrincipalEntity { get; set; }

}

public class Principal
{
    public int Id { get; set; }
    public ICollection<Dependent> DependentEntities { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Dependent> Dependents { get; set; }
    public DbSet<Principal> Principals { get; set; }
}

请注意你的数据库不会改变 - 底层的数据库一直为外键的列,但与独立的关联它没有暴露出来。

Note that your database won't change - the underlying database always had the column for the foreign key but with the independent association it was not exposed.

使用外键关联你可以通过改变外键的值更新的关系。如果你知道的价值,因为你不需要加载要更新的导航属性的实体这是非常方便。

With foreign key associations you can update the relationship just by changing the value of the foreign key. This is handy if you know the value because you don't need to load the entity you want to update the navigation property to.

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

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