EF code第4.3命名约定外键 [英] EF Code first 4.3 naming convention foreign key
问题描述
我有以下实体:
public class User
{
public int ID {get; set;}
public int GroupID {get; set;} // navigation property with
public Group Group {get; set;} // foreign key field
public Adress Adress {get; set;} // navigation property only
}
从实体框架生成的表如下所示:
The table generated from entity framework looks like:
ID
GroupID
Adress_ID
我不喜欢,该列命名为FK列是不一样的。我能做到这一点都使用相同的约定或者组ID,AdressID或GROUP_ID,Adress_ID?
I don't like, that the column naming for the FK columns is not the same. Can I achieve that both use the same convention either "GroupID, AdressID" or "Group_ID, Adress_ID"?
我使用约定优于配置,并且不希望使用流利的API。
I use convention over configuration and don't want to use Fluent API.
推荐答案
EF 4.1至4.3不支持创建自定义的约定,所以这是不可能的。你可以这样做(不流利API)的唯一的事情很可能映射外国财产另一列名:
EF 4.1 to 4.3 doesn't support creating custom conventions, so it's not possible. The only thing you can do (without Fluent API) is probably mapping the foreign property to another column name:
[Column("Group_ID")]
public int GroupID {get; set;}
然后你有两个FK列有下划线在数据库中。但是 - 正如你所看到的 - 你需要覆盖数据注解的惯例至少
Then you have both FK columns with underscore in the database. But - as you can see - you need to overwrite the conventions with data annotations at least.
定义第二FK列没有下划线只能用流利的API:
Defining the second FK column without underscore is only possible with Fluent API:
modelBuilder.Entity<User>()
.HasOptional(u => u.Address)
.WithMany()
.Map(x => x.MapKey("AddressID"));
这篇关于EF code第4.3命名约定外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!