简体   繁体   English

Angular - 使用 ASP.NET 内核从相关表中检索数据 Web ZDB974238714CA8DE634A7CE1DZF08A3

[英]Angular - retrieve data from related tables with ASP.NET Core Web API

I have many related tables with one to many relationship created with Entity Framework Core in ASP.NET Core Web API like this:我在 ASP.NET Core Web API 中创建了许多具有一对多关系的相关表,如下所示:

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; }
}

and so on....等等....

In Angular, I am trying to retrieve the data from code tables and its related tables在 Angular 中,我试图从代码表及其相关表中检索数据

In Angular I have an interface to simulate the data which I want to get (like view model)在 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
}

but I can't do it well?但我做不好? How can I get the data here?我怎样才能在这里获取数据?

I solved this by creating separated interface to every table and in the main table(Iproduct which refer to code table) create a property with type of it's related table example: for productMaterial table: IproductMaterial.interface.ts:我通过为每个表创建单独的接口并在主表(引用代码表的 Iproduct)中创建一个具有相关表示例类型的属性来解决这个问题:对于 productMaterial 表:IproductMaterial.interface.ts:

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

and in the main interface: Iproduct.interface.ts"并在主界面: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**
}

and by this way I could display the data easily通过这种方式,我可以轻松地显示数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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