Changes to a new entity with a two-level connection are not saved by Entity Framework.

entity-framework savechanges


I'm using the ADO.NET Entity Framework to create an ASP.NET MVC website. These entities are part of my entity model and are linked together through foreign keys:

ID, Date, Heading, Report Type ID, etc.

  • The link between SubReport and Report is one-to-one (ID, ReportText, etc.).
    • Sub Report and ReportSource(ID, Name, Description) have a one-to-many connection.
      • One-to-many link between ReportSource and ReportSourceType(ID, Name, Description).
      • Relationship between Contact (ID, Name, Address, etc.) and Report Source.

Every subreport type has its own Create.aspx page. A new Sub Report object is returned by the post event function.

Prior to that, I used the following post method:

  1. From the fields on the screen, set the attributes for a new Report object.
  2. From the fields on the page, modify the relevant characteristics of the SubReport object.
  3. Set the new Report entity established in 1 as the SubReport entity's Report.
  4. Look for the ReportSource using an ID supplied by the page, and then change the ReportSource of the Sub Report object to the entity you identified.
  5. SaveChanges.

This process worked well for a few weeks. Then, something changed last week, and it is no longer functional. I now get the following exception in place of the save operation:

UpdateException: "Entities in 'DIR2_5Entities.ReportSourceSet' 
participate in the 'FK_ReportSources_ReportSourceTypes' relationship. 
0 related 'ReportSourceTypes' were found. 1 'Report_Source_Types' is expected."

The following is shown by the debug visualizer:

  • The ReportSource for the SubReport has been loaded, configured, and all of its characteristics are accurate.
  • A legitimate ReportSourceType object is connected to the Report Source.

The prepared SQL query seems to be valid in SQL Profiler. Can somebody let me know what's clear that I'm overlooking?


Notes: In this situation, the Report and SubReport are always new entities. The Report entity, which is used for general inquiries, has attributes that are shared by many different sorts of reports. SubReports are particular reports with additional, type-specific characteristics. Although there is a separate entity set for each kind of SubReport, I'll use SubReport as a simplified example since this question relates to all of them.

8/17/2009 8:39:45 PM

Accepted Answer

If your database tables have a one-to-one connection with one another, then this often occurs. The reportsourceset in your example anticipates a reportsorttypes with the id it is referring. When my connection links two main keys from different tables together, I have encountered this issue.

12/2/2009 7:16:23 PM

Popular Answer

I apologize for being so late, but I just had a similar issue and spent about three hours trying to solve it. I would provide the code, but I can do it later if someone wants it as it is at home.

Here are a few things to look for:

  • Placing a breakpoint on the SaveChanges() method will allow you to carefully inspect the object context. You ought to see a list of the context's augmentations and modifications. When I initially checked, I discovered that it was attempting to point to rather than simply add all of my connected items. The context in your situation could be attempting to introduce a new Report Source Type.
  • The report source should be fetched from the database using its entity key and appropriately associated to the context, which is related to the preceding point. If not, your context could see it as a brand-new object, in which case its necessary associations won't be established.

I pulled my references from memory using thecontext.GetObjectByKey using the explicit attachThoseObjectsToContext function, and thencontext.Attach prior to assigning them to the properties of my first object's properties.

Related Questions


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