EF Code first 4.3 naming convention foreign key

谁说胖子不能爱 提交于 2019-12-22 18:26:15

问题


I have the following entity:

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

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

I use convention over configuration and don't want to use Fluent API.


回答1:


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

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.

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"));


来源:https://stackoverflow.com/questions/10258484/ef-code-first-4-3-naming-convention-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!