SQLite Error: FOREIGN KEY constraint failed in Entity Framework

Last Reply 6 months ago By dharmendr

Posted 6 months ago

I try to implement httpdelete but i get FK error

If i delete OrderItems and then Orders in sqlite all works great

With that configuration I get:

SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed'.

Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(int rc, sqlite3 db)

  • SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed'.

    • Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(int rc, sqlite3 db)

    • Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)

    • Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)

    • Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)

    • Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary<string, object> parameterValues, CancellationToken cancellationToken)

    • Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

As i understand in fluent Api need to implement configuration

builder
.HasMany(e => e.OrderItems)
.WithOne(e => e.Order)
.HasForeignKey(e => e.OrderItemId)
.OnDelete(DeleteBehavior.Cascade);

I read that the configuration should be placed only in one side right?

Not also in OrderItems configuration

You are viewing reply posted by: dharmendr 6 months ago.