[英]How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc
[英]How do i get foreign key table data using Entity Framework and Web Api in Asp.Net MVC
我的代码是:
public class EmployeeController : ApiController
{
ASITDbEntities db = new ASITDbEntities ( );
// GET api/employee
public IEnumerable<Employee> Get ( )
{
db.Configuration.ProxyCreationEnabled = false;
var result=db.Employees.ToList ()
return result.ToList ( );
}
}
我想从地区表中返回员工地区名称。 它仅返回区ID。
我尝试了db.Employee.Include(e=>e.district).Tolist();
但这没有用。 请帮我。
首先,您是先使用数据库还是先使用代码。 如果先编写代码,那么您的实体看起来像这样吗?
Public class Employee{
public int EmpId {get;set;}
public int EmpName {get;set;}
public int DistrictId {get;set;}
public District District {get;set;}
}
public class District{
public int DistrictId {get;set;}
public List<Employee> Employees {get;set;}
}
它在Employee和District实体之间创建1-1关系。 因此,现在如果包含db.Employee.Include(e=>e.district).Tolist()
,应返回完整的District对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.