這是內部異常:

InnerException: System.Data.UpdateException
Message=Entities in 'OrganizerDBEntities.Assignments' participate in 
the 'CourseId' relationship. 0 related 'Course' were found. 
1 'Course' is expected.`  

我有三個表(文件夾,作業,課程)。 Assignment表有一個名為AssignmentId的主鍵和一個名為CourseId的外鍵,其“Allow Nulls”屬性已設置為true。所以這個例外阻止我調用_entities.SaveChanges();從而阻止我向數據庫添加數據。


更新:感謝marc,問題解決了,但另一個問題出現了:

 InnerException: System.Data.UpdateException
        Message=An error occurred while updating the entries. See the InnerException for details.
        Source=System.Data.Entity
        StackTrace:
             at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
             at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
             at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)
             at System.Data.Objects.ObjectContext.SaveChanges()
             at AssignmentOrganizer.App_Data.AssignmentRepository.CreateAssignment(Assignment assignmentToCreate) in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\AssignmentOrganizer\AssignmentOrganizer\App_Data\AssignmentRepository.cs:line 19
             at AssignmentOrganizer.MainWindow..ctor() in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\AssignmentOrganizer\AssignmentOrganizer\MainWindow.xaml.cs:line 54
        InnerException: System.Data.EntityCommandCompilationException
             Message=An error occurred while preparing the command definition. See the inner exception for details.
             Source=System.Data.Entity
             StackTrace:
                  at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
                  at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
                  at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
                  at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
             InnerException: System.NotSupportedException
                  Message=Server-generated keys and server-generated values are not supported by SQL Server Compact.
                  Source=System.Data.SqlServerCe.Entity
                  StackTrace:
                       at System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree, ExpressionTranslator translator, DbExpression returning)
                       at System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, List`1& parameters, Boolean isLocalProvider)
                       at System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List`1& parameters, CommandType& commandType, Boolean isLocalProvider)
                       at System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
                       at System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
                       at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
                       at System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
                       at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
                  InnerException: 

一般承認的答案

我要檢查的第一件事是你的EF概念模型中的OrganizerDBEntities.Assignments協會。是否允許0:n關係?從錯誤消息,我導致相信它沒有。您可以在EF模型中調整基數(最小和最大可能值)。

如果在EDMX設計器表面中選擇兩個實體之間的關聯,則屬性窗口將顯示關聯的兩端。在每個“結束”屬性中,都有一個“多重性”值,它定義了關聯的基數: 0:*0..1:*1:*等等。

如果您確實想要允許缺失值,則關聯子端的多重性必須為0..1(零或一) - 它真的是0..1(零或一)?或者它設置為1而不是?


熱門答案

我的2美分。
我只是避免將外鍵值設置為NULL.So而不是在Assignment表中將CourseId設置為外鍵,將AssignmentId設置為Course表中的外鍵。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因