首页 > 解决方案 > Angular - 使用 ASP.NET Core Web API 从相关表中检索数据

问题描述

我在 ASP.NET Core Web API 中使用 Entity Framework Core 创建了许多具有一对多关系的相关表,如下所示:

public class code
{
    public int id { get; set; }
    public string productCode { get; set; }
    public bool direction { get; set; }
    public bool printFace { get; set; }
    public int number { get; set; }
    public int price { get; set; }
    public bool color { get; set; }
    public int typeId { get; set; }        

    public productType productType { get; set; }

    public int sizeId { get; set; }        

    public productSize productSize { get; set; }

    public int weightId { get; set; }        
    public productWeight productWeight { get; set; }

    public int materialId { get; set; }        
    public productMaterial productMaterial { get; set; }
}

public class productWeight
{
    public int id { get; set; }

    [Required]
    public int weight { get; set; }

    public List<code> codes { get; set; }
}

public class productType
{
    public int id { get; set; }

    [Required]
    public string typeName { get; set; }

    public virtual List<code> codes { get; set; }
}

等等....

在 Angular 中,我试图从代码表及其相关表中检索数据

在 Angular 中,我有一个接口来模拟我想要获取的数据(如视图模型)

export interface IProduct {
  id: number,
  productCode: string,
  direction: boolean,
  printFace: boolean,
  number: number,
  price: number,
  color: boolean,
  sizeId: number,
  typeId: number,
  weightId: number,
  materialId: number
}

但我做不好?我怎样才能在这里获取数据?

标签: angularentity-framework-coreasp.net-core-webapi

解决方案


我通过为每个表创建单独的接口并在主表(引用代码表的 Iproduct)中创建一个具有相关表示例类型的属性来解决此问题:对于 productMaterial 表:IproductMaterial.interface.ts:

export interface IproductMaterial{
  id:number,
  material:string
}

并在主界面:Iproduct.interface.ts"

export interface IProduct{
  id:number,
  productCode:string,
  direction:boolean,
  printFace:boolean,
  number:number,
  price:number,
  color:boolean,
  sizeId:number,
  typeId:number,
  weightId:number,
  materialId:number,
  **productMaterial:IproductMaterial**
}

通过这种方式,我可以轻松地显示数据


推荐阅读