EF新增多表管理數據時報錯:違反了引用完整性約束。Dependent Role 具備多個具備不一樣值的主體。程序員
最終問題解決後,徹底是由於本身的粗心,出了這樣的錯誤。數據庫
代碼以下: 服務器
public bool AddWithNotice(Announcement entity) { using (MyDbContext db = new MyDbContext()) { if (entity.OfMsg.Any()) { db.Msg.AddRange(entity.OfMsg.Select(s => s.Msg).ToList()); } db.Announcements.Add(entity); return db.SaveChanges() > 0; } }
寫了不少這樣的業務了,都是這樣處理的,這個也是這樣,完成了Repository的實現,準備測試下數據而後收工,帶着激動的心情填寫完數據後,提交,而後異常了!app
「/」應用程序中的服務器錯誤。 async
違反了引用完整性約束。Dependent Role 具備多個具備不一樣值的主體。 測試
說明: 執行當前 Web 請求期間,出現未經處理的異常。請檢查堆棧跟蹤信息,以瞭解有關該錯誤以及代碼中致使錯誤的出處的詳細信息。
異常詳細信息: System.Data.ConstraintException: 違反了引用完整性約束。Dependent Role 具備多個具備不一樣值的主體。
源錯誤: ui
行 35: //entity.OfMsg = null; google 行 36: db.Announcements.Add(entity); spa 行 37: return db.SaveChanges() > 0; 3d 行 38: } 行 39: return false; |
源文件: E:\Github\000\TRUMP\TRUMP.EF\Repository\AnnouncementRepository.cs 行: 37
堆棧跟蹤:
[ConstraintException: 違反了引用完整性約束。Dependent Role 具備多個具備不一樣值的主體。] System.Data.Entity.Core.Mapping.Update.Internal.KeyManager.GetPrincipalValue(PropagatorResult result) +334 System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildSetClauses(DbExpressionBinding target, PropagatorResult row, PropagatorResult originalRow, TableChangeProcessor processor, Boolean insertMode, Dictionary`2& outputIdentifiers, DbExpression& returning, Boolean& rowMustBeTouched) +262 System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildInsertCommand(PropagatorResult newRow, TableChangeProcessor processor) +105 System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler) +342 [UpdateException: 更新條目時出錯。有關詳細信息,請參閱內部異常。] System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler) +702 System.Data.Entity.Core.Mapping.Update.Internal.<ProduceDynamicCommands>d__a.MoveNext() +850 System.Linq.<ConcatIterator>d__59`1.MoveNext() +190 System.Data.Entity.Core.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable`1 commands, UpdateTranslator translator) +314 System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands() +174 System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +83 System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut) +9 System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(T noChangesResult, Func`2 updateFunction) +120 System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() +77 System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35() +11 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +288 System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) +163 System.Data.Entity.Core.Objects.<>c__DisplayClass2a.<SaveChangesInternal>b__27() +22 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +164 System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) +224 System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) +7 System.Data.Entity.Internal.InternalContext.SaveChanges() +114 [DbUpdateException: 更新條目時出錯。有關詳細信息,請參閱內部異常。] System.Data.Entity.Internal.InternalContext.SaveChanges() +199 System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +27 System.Data.Entity.DbContext.SaveChanges() +20 TRUMP.EF.Repository.AnnouncementRepository.AddWithNotice(Announcement entity) in E:\Github\000\TRUMP\TRUMP.EF\Repository\AnnouncementRepository.cs:37 TRUMP.Application.Service.AnnouncementService.AddWithNotice(AnnouncementDto model) in E:\Github\000\TRUMP\TRUMP.Application\Service\AnnouncementService.cs:40 TRUMP.MvcAdmin.Areas.Manager.Controllers.AnnouncementsController.Create(AnnouncementDto model) in E:\Github\000\TRUMP\TRUMP.MvcAdmin\Areas\Manager\Controllers\AnnouncementsController.cs:108 lambda_method(Closure , ControllerBase , Object[] ) +103 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22 System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +228 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9748493 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159 |
版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.7.2556.0
重試,重啓VS,仍是繼續報錯。
分析一下應該是哪裏關係配置有問題,本身檢查了一遍,沒發現哪裏不對。
去掉第35行註釋,保存成功,加上註釋報錯。明顯就是關聯表的問題,可是不管是調試跟蹤仍是其餘的方法都在細化異常,只獲得「違反了引用完整性約束。Dependent Role 具備多個具備不一樣值的主體。」這個比較模糊的錯誤,百度和google均沒有結果。
此時想打人的慾望11000%。強忍煩躁的心情(不知道爲何這麼煩躁),繼續調試一直到下班,沒解決!
實在沒有了興趣繼續圍着這麼莫名其妙的問題了,回家吃飯,飯後依然有些糾結,打開VS,F5仍是這錯誤。WTF,爲何我換了個地方還很差,什麼鬼!!!!!
下班前就拿着和另外一個模塊比對了,相同的業務邏輯,那個就好好的。也是怪了,雖然一直懷疑是實體關係配置錯誤,但是有仔細檢查了兩三遍配置的代碼沒發現問題。
這會吃過飯了,某寶某東上轉了下,購物車依然在嘲笑銀行卡的餘額。當年沒有直接買兩根8G條,如今一百個追悔莫及
打開Sql Server數據庫,看了下關聯表的外鍵和主鍵,突然以爲不對
what?好像有點問題哦
看了下Fluent Api配置的內容:
HasMany(s => s.OfMsg).WithRequired().HasForeignKey(s => s.MsgScheduleId);
你大爺的。把外鍵指定錯了~
搞了個大烏龍,折騰了有三個小時愣是沒解決。
再次爲本身的重啓VS,換電腦重啓解決方案以期能解決問題的愚蠢行徑感到好笑。我必定是一個假的程序員!