Asp.net 删除其他表中具有外键的项时出现问题
我试图删除一个产品项,但收到此错误消息。我知道我需要先删除库存表中的外键,然后才能从我的产品表中删除产品。我试图在我的modelbuilder中进行设置,但无法使其正常工作。。有什么好的解决办法吗 SqlException:DELETE语句与引用约束“FK__Stock__ProductID__2E1BDC42”冲突。冲突发生在数据库“Inventory”、表“dbo.Stock”、列“ProductID”中 这是我的Asp.net 删除其他表中具有外键的项时出现问题,asp.net,asp.net-core,Asp.net,Asp.net Core,我试图删除一个产品项,但收到此错误消息。我知道我需要先删除库存表中的外键,然后才能从我的产品表中删除产品。我试图在我的modelbuilder中进行设置,但无法使其正常工作。。有什么好的解决办法吗 SqlException:DELETE语句与引用约束“FK__Stock__ProductID__2E1BDC42”冲突。冲突发生在数据库“Inventory”、表“dbo.Stock”、列“ProductID”中 这是我的InventoryContext: public virtual Db
InventoryContext
:
public virtual DbSet<Products> Products { get; set; }
public virtual DbSet<Stock> Stock { get; set; }
public virtual DbSet<Storage> Storage { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Products>(entity =>
{
entity.HasKey(e => e.ProductId);
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.Price).HasColumnType("decimal(18, 0)");
});
modelBuilder.Entity<Stock>(entity =>
{
entity.Property(e => e.StockId).HasColumnName("StockID");
entity.Property(e => e.ProductId).HasColumnName("ProductID");
entity.Property(e => e.StorageId).HasColumnName("StorageID");
entity.HasOne(d => d.Product)
.WithMany(p => p.Stock)
.HasForeignKey(d => d.ProductId)
.HasConstraintName("FK__Stock__ProductID__2E1BDC42");
entity.HasOne(d => d.Storage)
.WithMany(p => p.Stock)
.HasForeignKey(d => d.StorageId)
.HasConstraintName("FK__Stock__StorageID__2F10007B");
});
modelBuilder.Entity<Storage>(entity =>
{
entity.Property(e => e.StorageId).HasColumnName("StorageID");
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(false);
});
}
}
}
公共虚拟数据库集产品{get;set;}
公共虚拟数据库集Stock{get;set;}
公共虚拟数据库集存储{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity(Entity=>
{
entity.HasKey(e=>e.ProductId);
entity.Property(e=>e.Name)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(假);
Property(e=>e.Price).HasColumnType(“decimal(18,0)”);
});
modelBuilder.Entity(Entity=>
{
Property(e=>e.StockId).HasColumnName(“StockId”);
Property(e=>e.ProductId).HasColumnName(“ProductId”);
entity.Property(e=>e.StorageId).HasColumnName(“StorageId”);
entity.HasOne(d=>d.Product)
.WithMany(p=>p.Stock)
.HasForeignKey(d=>d.ProductId)
.HASSCONSTRAINTNAME(“FK_uu库存产品ID_uu2e1bdc42”);
entity.HasOne(d=>d.Storage)
.WithMany(p=>p.Stock)
.HasForeignKey(d=>d.StorageId)
.HASSCONSTRAINTNAME(“FK_uuu库存存储ID_uuuu2F10007b”);
});
modelBuilder.Entity(Entity=>
{
entity.Property(e=>e.StorageId).HasColumnName(“StorageId”);
entity.Property(e=>e.Name)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(假);
});
}
}
}
这是我在控制器中的方法
public async Task<IActionResult> DeleteConfirmed(long id)
{
var products = await _context.Products.FindAsync(id);
_context.Products.Remove(products);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
public异步任务deleteConfixed(长id)
{
var products=wait_context.products.FindAsync(id);
_context.Products.Remove(产品);
wait_context.SaveChangesAsync();
返回重定向到操作(名称(索引));
}
我想您需要看看,外键是否可以为空?是否要在删除产品时删除子项(库存)?请参阅“嗨,我想在删除产品时删除库存…”。。我尝试过级联删除,但无法使我工作..好的,这就是级联删除..您是否像我尝试过的那样配置它,但仍然存在相同的问题。。。modelBuilder.Entity().HasOne(p=>p.Product).WithMany(b=>b.Stock).OnDelete(DeleteBehavior.Cascade);