Entity Framework Code First Relationship Without Foreign Key -


i'm new orm tools , want relationships between tables naming convention. example if have classes below;

public class city {     public int id;     public string name; }  public class district  {     public int id;     public string name;     public city city; } 

i want map these classes in database this.

city    id int,    name nvarchar(100)  district    id int,    name nvarchar(100),    cityid int 

i don't want use foreign keys , when put class property in class want migrate these property "classnameid". solution ?

your design relationship not correct use ef.

the correct relationship is

public class city {     public int id;     public string name;      public virtual icollection<district> districts; }  public class district  {     public int id;     public string name;     public int cityid;     public virtual city city; } 

and in entity configuration need make in city definition on entitytypeconfiguration :

  public districtconfiguration()         {             haskey(a => a.id);              hasrequired(a => a.city)                 .withmany(a => a.districts)                 .hasforeignkey(a => a.cityid);                totable("district");         } 

Comments