外键异常
这是内部异常:
InnerException: System.Data.UpdateException
Message=Entities in 'OrganizerDBEntities.Assignments' participate in
the 'CourseId' relationship. 0 related 'Course' were found.
1 'Course' is expected.`
我有三个表(文件夹、作业、课程)。分配表有一个名为AssignmentId 的主键和一个名为CourseId 的外键,其“Allow Nulls”属性已设置为true。所以这个异常阻止我调用 _entities.SaveChanges();从而阻止我向数据库添加数据。
更新:多亏了马克,这个问题已经解决了,但又出现了另一个问题:
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:
Here is the inner exception:
InnerException: System.Data.UpdateException
Message=Entities in 'OrganizerDBEntities.Assignments' participate in
the 'CourseId' relationship. 0 related 'Course' were found.
1 'Course' is expected.`
I have three tables (Folder, Assignment, Course). The Assignment table has a primary key called AssignmentId and a foreign key called CourseId whose 'Allow Nulls' property has been set to true. So this exception is preventing me calling _entities.SaveChanges(); and thus peventing me from adding data to the database.
UPDATE: Thanks to marc, That problem was solved, but another arose:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我要检查的第一件事是 EF 概念模型中的关联
OrganizerDBEntities.Assignments
。它允许 0:n 关系吗?从错误消息来看,我相信事实并非如此。您可以调整 EF 模型中的基数(最小和最大可能值)。如果您在 EDMX 设计器界面中选择两个实体之间的关联,属性窗口将显示关联的两端。在每个“End”属性中,都有一个“Multiplicity”值,用于定义关联的基数:
0:*
、0..1:*
、1:*
等等。如果您确实希望允许缺失值,则关联子端的重数需要为 0..1(零或一) - 它真的是 0..1(零或一)吗?还是将其设置为 1?
First thing I would check is the association
OrganizerDBEntities.Assignments
in your EF conceptual model. Does it allow for 0:n relations? From the error message, I am lead to believe it doesn't. You can tweak the cardinality (the min and max possible values) in your EF model.If you select an association between two entities in your EDMX designer surface, the properties window shows both ends of the association. In each of those "End" properties, there's a "Multiplicity" values which defines the cardinality of the association:
0:*
,0..1:*
,1:*
and so forth.If you do want to allow a missing value, the multiplicity on the child end of your association needs to be 0..1 (Zero or One) - is it really 0..1 (Zero or One) ? Or is it set to 1 instead?
这是我的 2 美分。
我只是避免将外键值设置为 NULL。因此,不要将 CourseId 设置为“分配”表中的外键,而是将“分配”设置为“课程”表中的外键。
Here my 2 cents.
I just avoid setting foreign key values to NULL.So instead of setting CourseId as a foreign key in Assignment table ,set AssignmentId as a foreign key in a Course table.