gpt4 book ai didi

entity-framework - Entity Framework 迁移级联删除始终为真,即使配置中的 WillCascadeOnDelete(false)

转载 作者:行者123 更新时间:2023-12-04 21:47:12 28 4
gpt4 key购买 nike

为什么与配置中定义的 WillCascadeOnDelete(false) 的关系在生成的迁移中始终为真?

这是配置代码

 public class PartySuppliesConfiguration:EntityTypeConfiguration<PartySupplies>
{
public PartySuppliesConfiguration()
{
HasKey(x => x.Id);
HasRequired(x=>x.Supplier).WithMany().HasForeignKey(x=>x.SupplierId).WillCascadeOnDelete(false);
HasRequired(x => x.Product).WithMany().HasForeignKey(x => x.ProductId).WillCascadeOnDelete(false);
HasRequired(x => x.Currency).WithMany().HasForeignKey(x => x.CurrencyId).WillCascadeOnDelete(false);
HasRequired(x => x.CreatedUser).WithMany().HasForeignKey(x => x.CreatedUserId).WillCascadeOnDelete(false);
Property(x => x.UnitPrice).HasColumnType("Money");
}
}

这是生成的迁移
 public override void Up()
{
CreateTable(
"PartySupplies",
c => new
{
Id = c.Int(nullable: false, identity: true),
SupplierId = c.Int(nullable: false),
ProductId = c.Int(nullable: false),
UnitPrice = c.Decimal(nullable: false, precision: 18, scale: 2),
CurrencyId = c.Int(nullable: false),
FromDate = c.DateTime(nullable: false),
ThruDate = c.DateTime(),
CreatedUserId = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("Parties", t => t.SupplierId, cascadeDelete: true)
.ForeignKey("Products", t => t.ProductId, cascadeDelete: true)
.ForeignKey("Curencies", t => t.CurrencyId, cascadeDelete: true)
.ForeignKey("Users", t => t.CreatedUserId, cascadeDelete: true)
.Index(t => t.SupplierId)
.Index(t => t.ProductId)
.Index(t => t.CurrencyId)
.Index(t => t.CreatedUserId);

}

最佳答案

这可能是一个愚蠢的问题,但是您是否覆盖了 DbContext 中的 OnModelBuilding 方法并添加了 modelBuilder.Configurations.Add( new PartySuppliesConfiguration( ) ); ?是否PartySuppliers是否有任何可能覆盖您的实体配置类的 DataAnnotations?或者它可能源自另一个类,其中cascadeondelete 设置为true?

关于entity-framework - Entity Framework 迁移级联删除始终为真,即使配置中的 WillCascadeOnDelete(false),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12553073/

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