sqlite 实体框架SQL错误19:‘外键约束失败’

wkyowqbh  于 2022-11-14  发布在  SQLite
关注(0)|答案(1)|浏览(343)

GitHub回购:https://github.com/blueboy90780/Coffee-POS-System/tree/master
我有一个使用.NET6和EF Core6.0的小型控制台应用程序,带有一个SQLite数据库。在运行我的代码时,我得到以下错误

Unhandled exception. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
 ---> Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'FOREIGN KEY constraint failed'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__104_0(DbContext _, ValueTuple`2 t)
   at Microsoft.EntityFrameworkCore.Storage.NonRetryingExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at Coffee_Shop_POS_System___EF_Core_6._0.MainProgram.Main() in /home/davidnguyen/Documents/Coffee Shop POS System/Coffee Shop POS System - EF Core 6.0/MainProgram.cs:line 61

Process finished with exit code 134.

错误消息指出它发生在MainProgram.cs文件内的第61行。这是什么代码?

database.SaveChanges();

我试过删除数据库和迁移文件夹,在原来的位置创建新的文件夹,并从备份副本重新填充数据库,但没有奏效。
为什么我的源代码抛出这个特定的错误?

zlhcx6iw

zlhcx6iw1#

这个错误说明了一些关于外键的事情,所以我会照顾你,你以正确的方式插入或删除。
例如:如果您试图删除And Item,则必须先在放置外键的表中删除它,然后才能在它与主键一起出现的表中删除它。
您尝试在您的开关柜中的mainProgram.cs文件中运行什么代码

相关问题