为什么EF5使用_Id创建外键,但建议使用不带下划线的命名约定ID? [英] Why does EF5 create foreign keys with _Id, but recommend naming convention Id without the underscore?
问题描述
喜欢这篇文章我不想在我的数据库中有两个约定。
代码首先使用模式[导航属性名称] _ [相关类的主键]
$在数据库中创建一个外键b $ b
为什么这两个明显不一致的约定?
持有对另一个实体的引用的每个实体都应该有属性名为 ForeignEntityId
(注意Id中的小写d)。
如果您按照确切的方式设置实体这个例子的模式,所有的列应该按照惯例命名:
public class Customer
{
public int Id {get;组; }
public string Name {get;组; }
public virtual ICollection< Order>订单{get;组;
}
public class Order
{
public int Id {get;组; }
public int CustomerId {get;组; } //这个属性很重要
public string ProductName {get;组; }
public decimal价格{get;组; }
public virtual Customer Customer {get;组; }
}
I have columns in my database that EF put in for me, that are classname followed by _Id. However much of the documentation recommends that the naming convention for foreign keys is the classname followed by Id without the underscore.
Like this post I dont want to have both conventions in my database.
Code First creates a foreign key in the database using the pattern [Name of navigation property]_[Primary Key of related class]
Why the 2 apparently inconsistent conventions?
Every entity that holds a reference to another entity should have a property named ForeignEntityId
(note the lower case d in Id).
If you set up your entities following the exact pattern of this example, all your columns should be named according to convention:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
public class Order
{
public int Id { get; set; }
public int CustomerId { get; set; } // this property is important
public string ProductName { get; set; }
public decimal Price { get; set; }
public virtual Customer Customer { get; set; }
}
这篇关于为什么EF5使用_Id创建外键,但建议使用不带下划线的命名约定ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!