asp.net mvc - Code first foreign keys -
within view, need return fullname every initiator , owner. so, should both initiator , owner foreign keys of employeeid?
i'm not sure how make both initiator , owner foreign keys using code first nor how display fullname using razor.
any ideas? thanks.
changerequest
namespace project.models { public class changerequest { public int changerequestid { get; set; } public int initiator { get; set; } public int owner { get; set; } } }
employee
namespace project.models { public class employee { public int employeeid { get; set; } public string surname { get; set; } public string firstname { get; set; } public string fullname { { return surname + ", " + firstname; } } } }
view
@foreach (var item in model.changerequests) { @html.displayfor(modelitem => item.initiator) @html.displayfor(modelitem => item.owner) }
you haven't mentioned ef version, if want lazy loading, or if using attributes or fluent api, here's reference many-valued assocations using fluent api , 1 using dataannotations, , bit of example code making assumptions version, lazy loading , attributes:
public class changerequest { [key] public int changerequestid { get; set; } public int? initiatorid { get; set; } public int? ownerid { get; set; } [foreignkey("initiatorid")] public virtual employee initiator { get; set; } [foreignkey("ownerid")] public virtual employee owner { get; set; } } public class employee { [key] public int employeeid { get; set; } public string surname { get; set; } public string firstname { get; set; } [inverseproperty("initiator")] public virtual icollection<changerequest> crsinitiated { get; set; } [inverseproperty("owner")] public virtual icollection<changerequest> crsowned { get; set; } [notmapped] public string fullname { { return surname + ", " + firstname; } } }
razor:
@foreach (var item in model.changerequests) { @html.displayfor(modelitem => item.initiator.fullname) @html.displayfor(modelitem => item.owner.fullname) }
Comments
Post a Comment