簡體   English   中英

使用實體框架C#WebApi從關系表中獲取數據

[英]Get data from relational tables using entity framework C# WebApi

我正在使用Webapi,並且數據庫和Department和Info表中都有兩個表。 部門表與信息表有關系。 我正在使用實體框架從數據庫中檢索數據,但是我僅從一個表INFO獲取數據,因此部門表顯示NULL,因此必須顯示部門數據,因為兩者都有關聯。

沒有Webapi,代碼將無法正常工作。 所以我錯了,為什么webapi中不顯示Department表。

希望您能理解我的問題,謝謝。

控制器

public class WebApiController : ApiController
{
    [HttpGet]
    public IHttpActionResult EmployeeList()
    {
       var List = DB.Infoes.ToList().OrderByDescending(x => x.ID);
       return Json(List);            
    }   
}

模型

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM