如何实现添加相关数据的方法EF core?需要将哪些对象提交给 add 方法,它应该返回什么?我需要在数据库中添加相关数据,例如:
{
"productId": 0,
"number": "xxx",
"amount": 5.65,
"primeCost": 20.33,
"productTypeId": 0,
"parameters": [
{
"id": 0,
"name": "Type",
"value": null
},
{
"id": 3,
"name": "steel grade",
"value": "CK45"
},
{
"id": 4,
"name": "diameter",
"value": "40"
}
]
}
这些是我的模型类:
public class Product //: BaseObject
{
public int Id { get; set; }
public string Name { get; set; }
public string Number { get; set; }
public double Amount { get; set; }
public double PrimeCost { get; set; }
[ForeignKey("ProductTypeId")]
public int ProductTypeId { get; set; }
public virtual ProductType ProductType { get; set; }
public ICollection<ProductParameter> ProductParameters { get; set; } = new List<ProductParameter>();
}
public class ProductType //: BaseObject
{
public int Id { get; set; }
public string NameType { get; set; }
public ICollection<Parameter> Parameters { get; set; } = new List<Parameter>();
public ICollection<Product> Products { get; set; } = new List<Product>();
}
public class Parameter //: BaseObject
{
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("ProductTypeId")]
public int ProductTypeId { get; set; }
public ProductType ProductType { get; set; }
public ICollection<ProductParameter> ProductParameters { get; set; } = new List<ProductParameter>();
}
public class ProductParameter //: BaseObject
{
public int Id { get; set; }
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public int ParameterId { get; set; }
public virtual Parameter Parameter { get; set; }
public string Value { get; set; }
}
守着一只汪
相关分类