c# - EF6, Composite Key, is annotation enough? Or Do I have to also use Fluent API? -
i have 3 entities:
public class aspnetuser { public string id {get; set;} } public class event { public int id {get; set;} } public class userevent { [column(order=0), key, foreignkey("aspnetusers")] public string userid { get; set; } [column(order=1), key, foreignkey("events")] public int eventid { get; set; } public datetime enroltime { get; set; } public virtual aspnetuser user { get; set; } public virtual event event { get; set; } }
as can see that, userevent relationship table has both userid , eventid 2 tables.
my question is:
- is annotation enough tell ef create 2 foreign keys? or have use fluent api in onmodelcreating() method in dbcontext class? or have create configuration class so? saw in post(it makes me confused):
entity framework multiple column primary key fluent api
- foreignkey("x") guess x should table name rather entity's name, right? e.g. x should aspnetusers(which in database), rather aspnetuser entity name.
thank you.
yes, it's enough using data annotations, problem need specify in foreignkey
attribute name of navigation property`that represents relationship foreign key for:
public class userevent { [ key,column(order=0), foreignkey("user")] public string userid { get; set; } [ key,column(order=1), foreignkey("event")] public int eventid { get; set; } public datetime enroltime { get; set; } public virtual aspnetuser user { get; set; } public virtual event event { get; set; } }
alternatively, can apply foreignkey
annotation navigation property , tell property foreign key relationship:
public class userevent { [key,column(order=0)] public string userid { get; set; } [key,column(order=1)] public int eventid { get; set; } public datetime enroltime { get; set; } [foreignkey("userid")] public virtual aspnetuser user { get; set; } [foreignkey("eventid")] public virtual event event { get; set; } }
Comments
Post a Comment