这是内部异常:
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:
答案 0 :(得分:5)
我要检查的第一件事是EF概念模型中的关联OrganizerDBEntities.Assignments
。是否允许0:n关系?从错误消息,我导致相信它没有。您可以在EF模型中调整基数(最小和最大可能值)。
如果在EDMX设计器表面中选择两个实体之间的关联,则属性窗口将显示关联的两端。在每个“结束”属性中,都有一个“多重性”值,用于定义关联的基数:0:*
,0..1:*
,1:*
等等。
如果您确实想要允许缺失值,则关联子端的多重性必须为0..1(零或一) - 它真的是0..1(零或一)?或者它被设置为1?
答案 1 :(得分:1)
这是我的2美分 我只是避免将外键值设置为NULL.So而不是在Assignment表中将CourseId设置为外键,将AssignmentId设置为Course表中的外键。