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

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

问题描述


可能重复:

代码第一:独立协会与外国关键协会?


在EF 4或EF 5 Code First中,什么是独立关联,什么是外键关联,如


2.4.1使用外键关联来减少视图生成成本



我们已经看到一些情况下,来自
模型 b
$ b所以 - 现在我知道使用哪个。如果只有我知道他们是什么,以及如何转换它!那么我的问题是,你如何定义这些术语?什么流利/注释/约定每个调用?

解决方案

外键协会是你的模型中有外键属性的地方到相应的导航属性。独立关联是当您的数据库中有一个外键列,但是与此列对应的外键属性不在您的模型中 - 即您具有NavigationProperty,但没有外键属性可以告诉您什么是ID值相关的财产并不真正涉及相关的财产。



以下是独立协会的模型示例(注意,Dependent没有外键 - 只是导航属性):

  public class Dependent 
{
public int Id {get;组;

[必需]
public Principal PrincipalEntity {get;组; }

}

public class Principal
{
public int Id {get;组; }
public ICollection< Dependent>依赖实体{get;组; }
}

public class MyContext:DbContext
{
public DbSet< Dependent>赡养者{get;组; }
public DbSet< Principal>校长{get;组; }
}

这里是一个相同模型的例子,但是与ForeignKey Association注意PrincipalEntity_Id属性和[ForeignKey()]属性):

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

public int PrincipalEntity_Id {get;组;

[必需]
[ForeignKey(PrincipalEntity_Id)]
public Principal PrincipalEntity {get;组; }

}

public class Principal
{
public int Id {get;组; }
public ICollection< Dependent>依赖实体{get;组; }
}

public class MyContext:DbContext
{
public DbSet< Dependent>赡养者{get;组; }
public DbSet< Principal>校长{get;组;
}

请注意,您的数据库不会更改 - 底层数据库始终具有列为外键,但与独立协会没有暴露。



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


Possible Duplicate:
Code First: Independent associations vs. Foreign key associations?

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 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?

解决方案

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

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天全站免登陆