简体   繁体   中英

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. 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.

Without Webapi the code is working fine. So where I am wrong and why does not Department table show in webapi.

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,否则你总是得到空值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM