database - Can I create multiple associations between two entities in Entity Framework 5, c#? -
e.g. have 2 entities:
public class department { public guid id { get; set; } public string name { get; set; } public icollection<employee> employees { get; set; } public employee manager { get; set; } } public class employee { public guid id { get; set; } public string fullname { get; set; } [foreignkey("departmentid")] public department department { get; set; } public guid departmentid { get; set; } public string position { get; set; } }
i know can associate (as did:) department.employees
employee.id
(and inverse: employee.department
department.id
).
questions:
1) 1 or 2 associations?
2) can create second association between department.manager
, employee.id
? don' want store managers in table stored in employee table , have in position
field "manager".
define relationship follows.
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<department>() .hasrequired(a => a.manager) .withmany() .hasforeignkey(a => a.employeeid); }
you want them virtual if want lazy loading , change tracking.
public class department { public guid id { get; set; } public string name { get; set; } public virtual icollection<employee> employees { get; set; } public virtual employee manager { get; set; } }
Comments
Post a Comment