asp.net mvc - Adding Relationships and Navigation Properties -
how go configuring or mapping navigation property?
i have employee table , display correctly employeestatusid
have employeestatus
text
along employeestatusid
how handle this?
i have following model:
employee.cs
public class employee { [key] public int? recordid { get; set; } public string company{ get; set; } public string name{ get; set; } public int? employeestatusid { get; set; } }
employeestatus:
public class employeestatus { public int id{ get; set; } public string name { get; set; } }
dbcontext:
public class datacenterdb : dbcontext { public dbset<employee> employees { get; set; } public dbset<employeestatus> employeestatus { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<employee>().totable("employee"); modelbuilder.entity<employeestatus>().totable("employeestatus"); base.onmodelcreating(modelbuilder); } }
in view use:
@html.displayfor(x => x.employeestatus.name)
this the employeestatus name value model. long relations correct entity framework automatically link them.
in model
public class employee { [key] public int recordid { get; set; } public string company{ get; set; } public string name{ get; set; } public int employeestatusid { get; set; } public virtual employeestatus employeestatus {get;set;} } public class employeestatus { public int id{ get; set; } public string name { get; set; } public virtual icollection<employee> employees { get; set; } }
i used keyword virtual in class knows link employee entity employeestatus entity. if it's foreign key relationship employeestatus id should not nullable
Comments
Post a Comment