【发布时间】:2021-07-18 05:28:43
【问题描述】:
我在 ASP.NET Core Web API 中使用 Entity Framework Core 和 PostGreSQL。我很难定义一组 A 类和 B 类的实体 包含 C 类的集合。
在数据库迁移期间生成的结果表/列不是我所期望的。
考虑以下表格定义:
public class Supplier
{
[Key]
public Guid SupplierId { get; set; }
public string SupplierName { get; set; }
public List<Commodity> Commodities { get; set; }
}
public class Consumer
{
[Key]
public Guid ConsumerId { get; set; }
public string ConsumerName { get; set; }
public List<Commodity> Commodities { get; set; }
}
public class Commodity
{
[Key]
public Guid CommodityId { get; set; }
public string CommodityName { get; set; }
}
这些类会生成以下表格/列:
Suppliers
SupplierId
SupplierName
Consumers
ConsumerId
ConsumerName
Commodities
CommodityId
CommodityName
ConsumerId
如何创建模型类定义以便生成的表/列保留这些关系?有没有办法强制创建连接表?
我应该使用一组特定的类/字段注释吗?
【问题讨论】:
标签: c# postgresql asp.net-core entity-framework-core