i have user entity in dbcontext. want users should able give references/leave comments each other, therefore have created reference entity.
public class reference { public virtual user { get; set; } // user leaves reference public virtual user { get; set; } // user has given reference public string opinions { get; set; } }
in user entity
public virtual icollection<reference> referencedto { get; set; } // collection of references user has given public virtual icollection<reference> referencedby { get; set; } // collection of references user has been given
what should make work either dataannonations or fluentapi, or how approach , solve?
you need this:
public class reference { public int referenceid { get; set; } public int byuserid { get; set; } public virtual user { get; set; } // user leaves reference public int touserid { get; set; } public virtual user { get; set; } // user has given reference public string opinions { get; set; } } public class user { public int userid { get; set; } public string name { get; set; } public virtual icollection<reference> referencedto { get; set; } // collection of references user has given public virtual icollection<reference> referencedby { get; set; } // collection of references user has been given } public class mycontext : dbcontext { public dbset<user> users { get; set; } public dbset<reference> references { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<reference>() .hasrequired(x => x.by) .withmany(x => x.referencedby) .hasforeignkey(x => x.byuserid) .willcascadeondelete(false); modelbuilder.entity<reference>() .hasrequired(x => x.to) .withmany(x => x.referencedto) .hasforeignkey(x => x.touserid) .willcascadeondelete(false); } }
Comments
Post a Comment