【发布时间】:2018-02-18 22:08:46
【问题描述】:
我正在构建一个模型,该模型代表可以在使用 EF Core 2.0 编写的电子商务平台中创建的典型产品。看下面的模型结构
public class GSProduct : BaseEntity
{
[Required]
public string Name { get; set; }
public ICollection<GSProduct> BaseProducts { get; set; }
public ICollection<GSRelatedProduct> ParentProducts { get; set; }
public ICollection<GSRelatedProduct> ChildProducts { get; set; }
public ICollection<GSVendorProductInfo> VendorProductInfo { get; } = new List<GSVendorProductInfo>();
}
public class GSRelatedProduct
{
public virtual GSProduct ParentProduct { get; set; }
public Guid ParentProductId { get; set; }
public virtual GSProduct ChildProduct { get; set; }
public Guid ChildProductId { get; set; }
}
public class GSVendorProductInfo : BaseEntity
{
public GSContact Vendor { get; set; }
public Guid VendorId { get; set; }
public GSProduct Product { get; set; }
public Guid ProductId { get; set; }
public string VendorPartNumber { get; set; }
public int BaseUnits { get; set; }
public double Cost { get; set; }
public int MinOrderQty { get; set; }
public int OrderedInMultiples { get; set; }
}
这是我为 Fluent API 设置的。
modelBuilder.Entity<GSVendorProductInfo>()
.HasOne(pt => pt.Product)
.WithMany(p => p.VendorProductInfo)
.HasForeignKey(pt => pt.ProductId);
modelBuilder.Entity<GSVendorProductInfo>()
.HasOne(pt => pt.Vendor)
.WithMany(t => t.VendorProductInfo)
.HasForeignKey(pt => pt.VendorId);
modelBuilder.Entity<GSRelatedProduct>().HasKey(x => new { x.ParentProductId, x.ChildProductId });
modelBuilder.Entity<GSRelatedProduct>()
.HasOne(pt => pt.ParentProduct)
.WithMany(t => t.ParentProducts)
.HasForeignKey(pt => pt.ParentProductId);
modelBuilder.Entity<GSRelatedProduct>()
.HasOne(pt => pt.ChildProduct)
.WithMany(t => t.ChildProducts)
.HasForeignKey(pt => pt.ChildProductId);
还包括迁移
migrationBuilder.CreateTable(
name: "GSRelatedProducts",
columns: table => new
{
ParentProductId = table.Column<Guid>(nullable: false),
ChildProductId = table.Column<Guid>(nullable: false),
Optional = table.Column<bool>(nullable: false),
Quantity = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_GSRelatedProducts", x => new { x.ParentProductId, x.ChildProductId });
table.ForeignKey(
name: "FK_GSRelatedProducts_GSProducts_ChildProductId",
column: x => x.ChildProductId,
principalTable: "GSProducts",
principalColumn: "Id",
onDelete: ReferentialAction.NoAction);
table.ForeignKey(
name: "FK_GSRelatedProducts_GSProducts_ParentProductId",
column: x => x.ParentProductId,
principalTable: "GSProducts",
principalColumn: "Id",
onDelete: ReferentialAction.NoAction);
});
脚手架/迁移工作正常,我实际上可以毫无问题地创建包含所有关系的产品。当我尝试将“RelatedProduct”添加到产品模型时出现问题。
我相应地设置了 ParentProductId 和 ChildProductId,当我创建或更新实体时,它会将 ParentProductId 和 ChildProductId 值都设置为 ParentProductId。
我已经通过调试器跟踪了代码,直到我调用 _context.Update(entity) 之前它都是正确的。之后,RelatedProduct 模型中的两个 Id 都设置为相同的值。
我不知道为什么会发生这种情况,任何建议都会很有帮助。
【问题讨论】:
-
您的类属性到表列名的映射是如何设置的?可能是您将父 ID 设置为两列吗?你能提供一些代码吗?
-
您指的是为表创建的迁移吗?
-
有多个父产品有什么理由吗?
-
这也有帮助。通常有一个映射类会说: Property(p => ParentId).HasColumnName("ParentId")
-
@Brad 父子产品联盟将是所有相关产品的列表。
标签: entity-framework asp.net-core