views:

580

answers:

2

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: 
+4  A: 

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?

marc_s
On the ends of my association is 1..* (Course to Assignment)
Mohit Deshpande
There you have your problem - the conceptual EF model requires a foreign key! That's why you're getting the error. Try to set that association to be 0:1..* instead
marc_s
Hold on. I recreated the model and now it is 0..1 --> * (from Course to Assignment)
Mohit Deshpande
And does it work now??
marc_s
No it does not work. It gives me another error >:-( Very frustrating. Check out the update
Mohit Deshpande
If it helps, on the edmx file, when I click on the Assignments table, the value/property column for the CourseId is blank. I'll give you almost any information you need.
Mohit Deshpande
Or could it just be my database, SQL Server Compact Edition (local-server)?
Mohit Deshpande
A: 

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.

Srinivas Reddy Thatiparthy