The exception is the foreign key.

c# entity-framework foreign-keys xamlparseexception

Question

The inner exception is as follows:

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

The three tables I have (Folder, Assignment, Course). AssignmentId serves as the primary key for the Assignment table, while CourseId serves as the foreign key, with its 'Allow Nulls' attribute set to true. Thus, I am unable to use _entities.SaveChanges() because of this issue, which prevents me from updating the database.


NEWSFLASH: Many thanks to Marc, Although that issue was resolved, a new one emerged:

 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: 
1
2
2/2/2010 1:51:21 AM

Accepted Answer

I would verify the association first.OrganizerDBEntities.Assignments in your EF theoretical framework. Does it support 0:n relationships? I get the impression that it doesn't based on the error message. In your EF model, you may change the cardinality (the minimum and maximum numbers that can be used).

In your EDMX designer surface, if you choose an association between two entities, the properties pane displays the association's two endpoints. There is a "Multiplicity" value that specifies the cardinality of the relationship in each of the "End" properties:0:* , 0..1:* , 1:* ... and so on.

The multiplicity on the kid end of your association must be 0..1 (Zero or One) if you really wish to permit a missing value. Is it actually 0..1 (Zero or One)? Or is it really set to 1?

5
1/31/2010 9:52:14 PM

Popular Answer

Here's my opinion.
I basically try to prevent foreign key values from being set to NULL. Therefore, put AssignmentId as a foreign key in a Course database rather than CourseId as a foreign key in the Assignment table.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow