什么是使用实体框架代码第一次创建时,外键的属性点? [英] What is the point of creating foreign key properties when using Entity Framework Code First?

查看:171
本文介绍了什么是使用实体框架代码第一次创建时,外键的属性点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在在本网站上的问题和答案的前瞻性和阅读一些关于代码优先发展前谷歌排名第一的教程我经常看到以下模式...

When looking at questions and answers on this site and reading some of the top Google-ranked tutorials on Code First development I frequently see the following pattern...

public class Category
{
    public Category()
    {
        Products = new Collection<Product>();
    }
    public Guid ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

public class Product
{
    public Guid ID { get; set; }
    public string Name { get; set; }
    public DateTime DateAdded { get; set; }
    public Guid CategoryID { get; set; } // Seemingly redundant property
    public virtual Category Category { get; set; }
}

当搜索代码首先教程以下两页上来,用下面这个相同的模式:

When searching for Code First tutorials the following two pages come up that use this same pattern:

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

http://www.codeproject.com/Articles/327945/Architecture-Guide-ASP-NET-MVC3-Entity-Framework-C

问:那么,什么是具有代码首先C#对象的外键属性点?在上面的例子中,你可以省略类别id 产品类,一切都将工作得很好。外键 CATEGORY_ID 仍然会在数据库中创建。

Question: So what is the point of having the foreign key property on the Code First C# object? In the above example you can omit CategoryID from the Product class and everything will work just fine. The foreign key Category_ID will still be created in the database.

我能想到的唯一的事情是,人们可能希望能够指定的关系是否是可选的使用可空类型,而不是流畅的API,但我认为这真的混淆的东西既有类别类别id 属性。

The only thing I could think of is that people might like to be able to specify whether the relationship is optional using nullable types instead of the fluent API, but I think it really confuses things to have both a Category and CategoryID property.

所以我才去走一走,并删除所有我的外键的属性,有什么,我在这里失踪?什么是这样做的意义呢?

So before I go around and remove all my foreign key properties, is there anything that I'm missing here? What's the point of doing this?

谢谢!

推荐答案

是的,我认为没有必要为外键的属性,他们在某种程度上,在对象世界关系的神器。您可以完全定义的关系,而不FK属性。在流利的API,你可以定义如果关系是可选的还是必需的,你可以指定数据库表的外键列名。然后的关系被称为独立协会

Yes, I think there is no need for foreign key properties and they are somehow a relational artifact in the object world. You can define relationships completely without FK properties. In Fluent API you can define if the relationship is optional or required and you can specify the database table's foreign key column name. The relationship is then called Independent Association.

我的理解是,外键关联 - 有外露的外键关系在你的模型类的属性 - 的存在只是为了让与实体框架更容易一点,并在某些情况下更舒适的工作关系。例如:

My understanding is that Foreign Key Associations - relationships with exposed foreign key properties in your model class - only exist to make working with relationships in Entity Framework a bit easier and more comfortable in certain scenarios. For example:

假设你有一个Web视图中创建或编辑的产品和视图包含一个组合框选择一个类别,并将其分配给该产品。为了填补组合框,当视图显示你加载,例如, ID 名称所有类别从数据库中。

Suppose you have a web view to create or edit a product and the view contains a combobox to select a category and assign it to the product. To fill the combobox when the view is rendered you would load, for instance, the ID and the Name of all categories from the database.

在页面被调回您将收到的属性,产品和 ID 的选定的类别。如果你没有在你的产品外键属性类别id 您必须创建关系是这样的:

When the page is posted back you would receive the properties for the product and the ID of the selected category. If you don't have a foreign key property CategoryID in your Product you would have to create the relationship this way:

var category = new Category { ID = IDFromComboBox };
context.Categories.Attach(category);
product.Category = category;



随着FK属性,你只需要一行:

With a FK property you only need one line:

product.CategoryID = IDFromComboBox;



外键的属性没有在实体框架版本1(.NET 3.5)存在,并已推出与EF版本4(.NET 4)支持象上面更好的方案。

Foreign key properties didn't exist in Entity Framework version 1 (.NET 3.5) and have been introduced with EF version 4 (.NET 4) to support scenarios like the above better.

这是外键关联的一个关键观点可以发现,两种类型之间的区别协会在拉吉斯拉夫的博客很好的讨论:

A critical view on Foreign Key Association can be found and the difference between the two types of associations is very well discussed in Ladislav's blog:

http://www.ladislavmrnka.com/2011/05/foreign-key-vs-independent-associations-in-ef-4/

这篇关于什么是使用实体框架代码第一次创建时,外键的属性点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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