[英]Get data from relational tables using entity framework C# WebApi
I am using Webapi and i have two tables in database and Department and Info tables. 我正在使用Webapi,并且数据库和Department和Info表中都有两个表。 Department table has relation with Info table.
部门表与信息表有关系。 I am using entity framework to retrieve data from database but I am getting data from only one table INFO so the department table shows NULL it has to show department data because both have relation.
我正在使用实体框架从数据库中检索数据,但是我仅从一个表INFO获取数据,因此部门表显示NULL,因此必须显示部门数据,因为两者都有关联。
Without Webapi the code is working fine. 没有Webapi,代码将无法正常工作。 So where I am wrong and why does not Department table show in webapi.
所以我错了,为什么webapi中不显示Department表。
I hope you understand my question thanks. 希望您能理解我的问题,谢谢。
Contorller 控制器
public class WebApiController : ApiController
{
[HttpGet]
public IHttpActionResult EmployeeList()
{
var List = DB.Infoes.ToList().OrderByDescending(x => x.ID);
return Json(List);
}
}
Model 模型
public partial class Info
{
public int ID { get; set; }
public string Image_Name { get; set; }
public string First_Name { get; set; }
public string Last_name { get; set; }
public string Desription { get; set; }
public Nullable<int> DeprtmentIDFK { get; set; }
public virtual Department Department { get; set; }
}
public partial class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public virtual ICollection<Info> Infoes { get; set; }
}
由于它是讨论在这里 ,为了获取你必须导航属性Include
他们在您的IQueryable,否则你总是得到空值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.