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.
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