You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EF Core 9.0.2删除Album时触发SQLite外键约束失败求助

EF Core 9.0.2数据库优先模式下删除Album条目触发SQLite外键约束失败问题

我正在使用EF Core 9.0.2(数据库优先模式),基于未修改的SQLite示例数据库chinook.db开展测试。

以下是OnModelCreating中的配置代码:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Album>(entity =>
    {
        entity.HasIndex(e => e.ArtistId, "IFK_AlbumArtistId");

        entity.Property(e => e.Quantity).HasDefaultValue(0);
        entity.Property(e => e.Title)
            .IsRequired()
            .HasColumnType("NVARCHAR(160)");

        entity.HasOne(d => d.Artist).WithMany(p => p.Albums)
            .HasForeignKey(d => d.ArtistId)
            .OnDelete(DeleteBehavior.ClientSetNull);
    });
}

当我删除Album条目时,抛出如下异常:

SQLite Error 19: 'FOREIGN KEY constraint failed'

已做排查:

  • 检查原始数据库,确认无异常
  • 查阅多份相关异常报告,仍无法解决问题

数据库架构图如下:
数据库架构图

内容的提问来源于stack exchange,提问作者Brummell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 22:12:02