简体   繁体   English

SQLite 错误 19:“外键约束失败”

[英]SQLite Error 19: 'FOREIGN KEY constraint failed'

I am working on a C# application, and i am using entity framework to handle the database.我正在开发一个 C# 应用程序,并且我正在使用实体框架来处理数据库。 I have the following DB design in my project:我的项目中有以下数据库设计:

数据库设计

I get a "Foreign Key Constraint Failed" error whenever i try do delete a row in the "rentals" table, and i cant figure out why.每当我尝试删除“rentals”表中的一行时,我都会收到“Foreign Key Constraint Failed”错误,但我不知道为什么。

My migration for the rentals table looks like this:我对出租表的迁移如下所示:

    migrationBuilder.CreateTable(
    name: "Rentals",
    columns: table => new
    {
        Id = table.Column<int>(type: "INTEGER", nullable: false)
            .Annotation("Sqlite:Autoincrement", true),
        CustomerId = table.Column<int>(type: "INTEGER", nullable: false),
        ProductsId = table.Column<int>(type: "INTEGER", nullable: false),
        DateAssigned = table.Column<DateTime>(type: "TEXT", nullable: false),
        Active = table.Column<bool>(type: "INTEGER", nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Rentals", x => x.Id);
        table.ForeignKey(
            name: "FK_Rentals_Customers_CustomerId",
            column: x => x.CustomerId,
            principalTable: "Customers",
            principalColumn: "Id");
        table.ForeignKey(
            name: "FK_Rentals_Products_ProductId",
            column: x => x.ProductId,
            principalTable: "Products",
            principalColumn: "Id");
    });    

modelBuilder.Entity("API.Entities.Rentals", b =>
    {
        b.HasOne("API.Entities.Customer", "Customer")
            .WithMany("Rentals")
            .HasForeignKey("CustomerId")
            .OnDelete(DeleteBehavior.NoAction)
            .IsRequired();

        b.HasOne("API.Entities.Product", "Product")
            .WithMany("Rentals")
            .HasForeignKey("ProductId")
            .OnDelete(DeleteBehavior.NoAction)
            .IsRequired();

        b.Navigation("Customer");

        b.Navigation("Product");
    });

Any ideas on what i am doing wrong?关于我做错了什么的任何想法?

Seem to be a glitch.好像是个毛病。

Restarted VS and afterwards the error didnt appear again.重新启动VS,之后错误不再出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLite 异常:SQLite 错误 19:“外键约束失败” - SQLite Exception: SQLite Error 19: “Foreign Key Constraint failed” EF Core SQLite 错误 19:“外键约束失败”尽管配置了一对多 - EF Core SQLite Error 19: 'FOREIGN KEY constraint failed' despite configuring one to many SQLite EF Core -“外键约束失败” - SQLite EF Core - 'FOREIGN KEY constraint failed' SQLite 单元测试中的 FOREIGN KEY 约束失败 - FOREIGN KEY constraint failed in SQLite unit tests EF Core SQLITE - SQLite 错误 19:&#39;唯一约束失败 - EF Core SQLITE - SQLite Error 19: 'UNIQUE constraint failed SQLite 错误 19:&#39;唯一约束失败:PosterDto.Id&#39; - SQLite Error 19: 'UNIQUE constraint failed: PosterDto.Id' ASP.NET Core 3.1 将数据发布到相关表(一对多)以 sqllite 错误 19 '外键约束失败告终 - ASP.NET Core 3.1 posting data to related tables(one to many) ending up with sqllite error 19 'Foreign key constraint failed EF core 6.0 + SQLite 外键约束,有意义的错误描述 - EF core 6.0 + SQLite foreign key constraint, meaningful error description SQLite 错误 19:'NOT NULL 约束失败' - 默认情况下,.NET6/C# 8 中的字符串可以为空吗? - SQLite Error 19: 'NOT NULL constraint failed' - Are strings nullable by default in .NET6/C# 8? 使用SQLite和C#实施外键约束 - Enforce foreign key constraint with SQLite & C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM