While looking for a good solution to save a detached object tree using Entity Framework, I found the perfect solution:GraphDiff.
GraphDiff offers a set of DbContext extension methods for Entity Framework Code First, that allow you to save an entire detached Model/Entity, with child Entities and Lists, to the database without writing the code to do it. It has the following features to offer:
More information here: http://blog.brentmckendrick.com/introducing-graphdiff-for-entity-framework-code-first-allowing-automated-updates-of-a-graph-of-detached-entities/
This is a must have for every Entity Framework user!
GraphDiff offers a set of DbContext extension methods for Entity Framework Code First, that allow you to save an entire detached Model/Entity, with child Entities and Lists, to the database without writing the code to do it. It has the following features to offer:
- Merge an entire graph of detached entities to the database using DbContext.UpdateGraph();
- Ensures concurrency is maintained for all child entities in the graph
- Allows for different configuration mappings to ensure that only changes within the defined graph are persisted
- Comprehensive testing suite to cover many (un/)common scenarios.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route()] | |
public IHttpActionResult Post(RoleAssignment roleAssignment) | |
{ | |
//Existing role | |
_context.UpdateGraph(roleAssignment, map => map | |
.OwnedCollection(r => r.ActiveDirectoryItems) | |
.OwnedEntity(r => r.Role, withRole => withRole | |
.OwnedCollection(r => r.Operations) | |
.OwnedCollection(r => r.Tasks, withTask => withTask | |
.OwnedCollection(t => t.Operations)))); | |
_context.SaveChanges(); | |
return Ok(); | |
} |
More information here: http://blog.brentmckendrick.com/introducing-graphdiff-for-entity-framework-code-first-allowing-automated-updates-of-a-graph-of-detached-entities/
This is a must have for every Entity Framework user!