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:
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?