gpt4 book ai didi

entity-framework-migrations - 当 key 位于基类中时,EF 迁移 DropForeignKey 失败

转载 作者:行者123 更新时间:2023-12-04 13:20:26 25 4
gpt4 key购买 nike

我正在尝试为 EF 5.0.0-rc 代码优先类库更新我的数据模型。 Up() 方法中的第一个命令是

DropForeignKey("dbo.ChileInventory", new[] { "LotInventoryItemTypeId", "ChileProductId" }, "dbo.ChileProducts");

但我收到 SqlException: 'FK_dbo.ChileInventory_dbo.ChileProducts_LotInventoryItemTypeId_ChileProductId' 不是约束。无法删除约束。

我认为错误的原因是由于我的 ChileProducts 类从其基类中获取了它的关键属性。由于接受主列名称的 DropForeignkey 方法没有重载,我相信 EF 无法确定要删除的正确约束。我还应该指出,异常消息中显示的约束名称与数据库中的约束名称不匹配(因此出现错误......)

您将在下面找到数据模型和迁移方法。但首先简要说明一下迁移背后的变化性质:InventoryBase 类的每个派生类都通过 InventoryTypeId 和 [InventoryTypeSpecific]ProductId 的复合键定义产品类型。例如,ChileInventory 会将其特定的智利类型标识为 InventoryItemTypeId = [ChileInventoryTypeId] 和 ChileProductId = [ChileProductId]。 PackagingInventory 会将其包装类型标识为 InventoryItemTypeId = [PackagingInventoryTypeId] 和 PackagingProductId = [PackagingProductId]。等等。

我正在努力推出的模型更改是将 [InventoryTypeSpecific]ProductId 从每个 InventoryBase 衍生品提升到基础。这将导致所有 InventoryBase 派生对象共享一个公共(public) ProductId 属性,该属性与 InventoryItemTypeId 一起可以实现从 InventoryBase 到 ProductBase 的导航;这在以前的模型中是不可能的。

提前感谢您的建议和考虑。
——文尼

数据模型

智利产品
public abstract class ProductBase
{
[Key]
[Column(Order = 0)]
public virtual int InventoryItemTypeId { get; set; }

[Key]
[Column(Order = 1)]
public virtual int Id { get; set; }

[StringLength(150)]
public virtual string Name { get; set; }

[ForeignKey("InventoryItemTypeId")]
public virtual InventoryItemType InventoryItemType { get; set; }

public virtual bool IsActive { get; set; }
}
public class ChileProduct : ProductBase
{
public virtual int ChileTypeId { get; set; }

[ForeignKey("ChileTypeId")]
public virtual ChileType ChileType { get; set; }
}

智利库存
public abstract class InventoryBase
{
[Key]
[Column(Order = 0, TypeName = "Date")]
public virtual DateTime DateCreated { get; set; }

[Key]
[Column(Order = 1)]
public virtual int Sequence { get; set; }

[Key]
[Column(Order = 2)]
public virtual int LotInventoryItemTypeId { get; set; }

[Column(TypeName = "Date")]
public virtual DateTime LotDateCreated { get; set; }

public virtual int LotSequence { get; set; }

public virtual int ProductId { get; set; }

[ForeignKey("LotInventoryItemTypeId, LotDateCreated, LotSequence")]
public virtual Lot Lot { get; set; }

[ForeignKey("LotInventoryItemTypeId")]
public virtual InventoryItemType ItemType { get; set; }

public virtual ICollection<InventoryQuantityByLocation> QuantitiesByLocation { get; set; }

[ForeignKey("LotInventoryItemTypeId, ProductId")]
public virtual ProductBase ProductBase { get; set; }
}

public class ChileInventory : InventoryBase
{
[Column(TypeName = "Date")]
public virtual DateTime? ProductionBatchDateCreated { get; set; }

public virtual int? ProductionBatchSequence { get; set; }

public virtual int PackagingInventoryItemTypeId { get; set; }

public virtual int PackagingProductId { get; set; }

[ForeignKey("ProductionBatchDateCreated, ProductionBatchSequence")]
public virtual ProductionBatch ProductionBatch { get; set; }

[ForeignKey("LotInventoryItemTypeId, ProductId")]
public virtual ChileProduct ChileProduct { get; set; }

[ForeignKey("PackagingInventoryItemTypeId, PackagingProductId")]
public virtual PackagingProduct PackagingProduct { get; set; }

}

移民
public override void Up()
{
DropForeignKey("dbo.ChileInventory", new[] { "LotInventoryItemTypeId", "ChileProductId" }, "dbo.ChileProducts");
DropForeignKey("dbo.PackagingInventory", new[] { "LotInventoryItemTypeId", "PackageId" }, "dbo.PackagingProducts");
DropForeignKey("dbo.AdditiveInventory", new[] { "LotInventoryItemTypeId", "AdditiveProductId" }, "dbo.AdditiveProducts");
DropIndex("dbo.ChileInventory", new[] { "LotInventoryItemTypeId", "ChileProductId" });
DropIndex("dbo.PackagingInventory", new[] { "LotInventoryItemTypeId", "PackageId" });
DropIndex("dbo.AdditiveInventory", new[] { "LotInventoryItemTypeId", "AdditiveProductId" });
AddColumn("dbo.Inventory", "ProductId", c => c.Int(nullable: false));
AddForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts", new[] { "InventoryItemTypeId", "Id" });
AddForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts", new[] { "InventoryItemTypeId", "Id" });
AddForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts", new[] { "InventoryItemTypeId", "Id" });
AddForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts", new[] { "InventoryItemTypeId", "Id" });
CreateIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
CreateIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
CreateIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
CreateIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
DropColumn("dbo.ChileInventory", "ChileProductId");
DropColumn("dbo.PackagingInventory", "PackageId");
DropColumn("dbo.AdditiveInventory", "AdditiveProductId");
}

public override void Down()
{
AddColumn("dbo.AdditiveInventory", "AdditiveProductId", c => c.Int(nullable: false));
AddColumn("dbo.PackagingInventory", "PackageId", c => c.Int(nullable: false));
AddColumn("dbo.ChileInventory", "ChileProductId", c => c.Int(nullable: false));
DropIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
DropIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
DropIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
DropIndex("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" });
DropForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts");
DropForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts");
DropForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts");
DropForeignKey("dbo.Inventory", new[] { "LotInventoryItemTypeId", "ProductId" }, "dbo.AdditiveProducts");
DropColumn("dbo.Inventory", "ProductId");
CreateIndex("dbo.AdditiveInventory", new[] { "LotInventoryItemTypeId", "AdditiveProductId" });
CreateIndex("dbo.PackagingInventory", new[] { "LotInventoryItemTypeId", "PackageId" });
CreateIndex("dbo.ChileInventory", new[] { "LotInventoryItemTypeId", "ChileProductId" });
AddForeignKey("dbo.AdditiveInventory", new[] { "LotInventoryItemTypeId", "AdditiveProductId" }, "dbo.AdditiveProducts", new[] { "InventoryItemTypeId", "Id" });
AddForeignKey("dbo.PackagingInventory", new[] { "LotInventoryItemTypeId", "PackageId" }, "dbo.PackagingProducts", new[] { "InventoryItemTypeId", "Id" });
AddForeignKey("dbo.ChileInventory", new[] { "LotInventoryItemTypeId", "ChileProductId" }, "dbo.ChileProducts", new[] { "InventoryItemTypeId", "Id" });
}

最佳答案

这是我发现的一种解决方法:

使用包含参数 principalName 和 name 的 DropForeignKey 重载——在这种情况下意味着 约束 姓名!它有点脆弱,因为它需要预先了解约束名称,但它可以按预期工作。

关于entity-framework-migrations - 当 key 位于基类中时,EF 迁移 DropForeignKey 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11528476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com