小编Bou*_*ieb的帖子

FK 和 PK 实体框架核心之间的冲突

我试图建立与实体框架核心的Web应用程序,我创建了两个型号CategoryPie,我做的所有事情,包括的DbContext的依赖注入,我创建了一个DbInializer类来检查,如果数据库是空的,如果这是真的,那会插入一些数据,问题是当我运行应用程序时出现异常,好像类别表中的主键和 Pies 表中的外键之间存在冲突,这是异常:

 System.Data.SqlClient.SqlException: The MERGE statement conflicted with the FOREIGN KEY constraint "FK_Pies_Categories_CategoryId".The conflict occurred in database "ShopDb", table "dbo.Categories", column 'CategoryId'.
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean & dataReady)
   at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean & moreRows)
   at System.Data.SqlClient.SqlDataReader.TryHasMoreResults(Boolean & moreResults)
   at System.Data.SqlClient.SqlDataReader.TryNextResult(Boolean & more)
   at System.Data.SqlClient.SqlDataReader.NextResult()
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(DbDataReader reader)
ClientConnectionId: 775a7294 - 531a - 44cc …
Run Code Online (Sandbox Code Playgroud)

c# sql-server entity-framework asp.net-core-mvc

1
推荐指数
1
解决办法
1666
查看次数