My question seems similar to this one but the exact details as well as the solution seem to have changed over the years. (For one thing, the ObjectStateManager
referenced in the accepted answer is apparently no longer accessible.)
I have a web app that displays an object graph. One of the objects in the graph is a list. The user can edit the items in the list. They can also add new items. When they save their changes, I serialize the whole object graph (including a field indicating whether values were added or modified) and send it to a controller to update the database. I send the added items with uninitialized IDs. In the controller, I deserialize the object graph and pass it to DbContext#Attach()
. Then I set the entity states of the attached entries before calling SaveChanges()
.
This works with any number of changed rows, and it works with a single added row. The database assigns an ID when a row is inserted. But Attach()
does not accept multiple added rows with the same uninitialized ID.
Is there a way to suppress the validation of IDs in Attach()
? It seems like EF should leave that up to the database. Failing that, how should I go about this?