.net EF Code first 4.3命名约定外键
我拥有以下实体:.net EF Code first 4.3命名约定外键,.net,entity-framework,ef-code-first,entity-framework-migrations,.net,Entity Framework,Ef Code First,Entity Framework Migrations,我拥有以下实体: 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
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
}
实体框架生成的表如下所示:
ID
GroupID
Adress_ID
我不喜欢FK列的列命名不一样。我能做到两者都使用相同的约定“GroupID,AdressID”或“Group\u ID,Adress\u ID”吗
我使用约定而不是配置,不想使用Fluent API。EF 4.1到4.3不支持创建自定义约定,因此不可能。在没有Fluent API的情况下,您可以做的唯一一件事可能是将外部属性映射到另一个列名:
[Column("Group_ID")]
public int GroupID {get; set;}
然后,数据库中有两个带下划线的FK列。但是,正如您所看到的,您至少需要用数据注释覆盖约定
只有使用Fluent API才能定义不带下划线的第二个FK列:
modelBuilder.Entity<User>()
.HasOptional(u => u.Address)
.WithMany()
.Map(x => x.MapKey("AddressID"));
modelBuilder.Entity()
.has可选(u=>u.Address)
.有很多
.Map(x=>x.MapKey(“AddressID”);
http://stackoverflow.com/questions/18050590/why-does-ef5-create-foreign-keys-with-id-but-recommend-naming-convention-id-wi/18066373?noredirect=1#18066373
解释了该列出现的原因