I have created a model
public class Moo
{
public Int32 MooId { get; set; }
public string Description { get; set; }
public City CityIdFrom { get; set; }
public Int32 Weight { get; set; }
}
and
public class City
{
public Int32 CityId { get; set; }
public string Name { get; set; }
}
And I wrote
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(
new System.Data.Entity.DropCreateDatabaseAlways<DbConnection>());
...
in Global.asax.cs
In short – I followed the tutorial in here.
However, I get a database table Moo created with columns as follows:
[MooId]
,[Description]
,[Weight]
,[CityIdFrom_CityId]
but, as it was in the tutorial, I expected this look as:
[MooId]
,[Description]
,[Weight]
,[CityIdFrom]
What is the reason the generator is adding unnecessary _CityId? (same goes for all the tables’ columns with relations) I am pretty sure there has to be a setting, but I find it hard to word my problem.
You’re not explicitly declaring the FK column in your
Moomodel.The
CityIdFromproperty is a navigation property of typeCity, not a FK column of typeInt32.So, the column you’re seeing in the database is created for you by EF automatically.
If you want to name this column, you have to add it to your model:
Note: the column names are by convention so EF can figure out what goes with what.
So
CityFromIdis the database FK column for theCityFromnavigation property because it has the same name withIdat the end.