Why am I getting an extra foreign key column with Entity Framework Code First Foreign Key Attributes? -
i came across strange problem entity framework code first.
my class looks this
public class status { [key] public int statusid { get; set; } public string name { get; set; } public int memberid { get; set; } [foreignkey("memberid")] public virtual member member { get; set; } public int posterid { get; set; } [foreignkey("posterid")] public virtual member poster { get; set; } public virtual icollection<statuslike> statuslikes { get; set; } public virtual icollection<statuscomment> statuscomments { get; set; } }
my member class looks this
public class member { [key] public int memberid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string bio { get; set; } public virtual icollection<membercoursetaken> membercoursetakens { get; set; } public virtual icollection<status> statuses { get; set; } public virtual icollection<club> foundedclubs { get; set; } public string emailaddress { get; set; } public string password { get; set; } public string phone { get; set; } public int accountsourceid { get; set; } public accountsource accountsource { get; set; } public int addressid { get; set; } public address address { get; set; } public string profilephoto { get; set; } public int memberrankid { get; set; } public memberrank memberrank { get; set; } public datetime created { get; set; } public datetime modified { get; set; } }
and whatever reason database table created has following columns
statusid name memberid posterid member_memberid
with memberid
, posterid
, , member_memberid
being foreign keys.
how can keep member_memberid
being generated?
your member_memberid
column created because of member.statuses
property. can imagine not want. members , statuses should exist independent of each other, need junction table.
i don't know if use onmodelcreating
override of dbcontext, that's place change mapping between member , status:
protected override void onmodelcreating(dbmodelbuilder mb) { mb.entity<member>().hasmany(m => m.statuses).withmany(); }
this create table memberstatuses table 2 id columns foreign keys. way model many-to-many relationship without navigation property on "other" side of association. (i don't think want members
property in status
).
Comments
Post a Comment