Looking for a complete solution for automatically updating a graph of entities using the Entity Framework? Read On!
Hi, As usual I have neglected this blog as of late. It is getting harder and harder to find time to put some notes up here. But hopefully today I have something very interesting to make up for it.
So today I’m finally going to post something that we have actually been using live on our production code for quite some time, and the good news is that it is working beautifully. I’m introducing GraphDiff – an extension method allowing for the automatic update of a detached graph using Entity Framework code first. (Edit: code has been rewritten to handle multiple new features, as such no guarantee can be given on its production-ready usage, but I’m continuing to work on it to make sure it is relatively bug-free)
Working with detached graphs of entities we quite often found that it was cumbersome to use the Entity Framework to manually map all of the changes from an aggregate root to the database. By aggregate root I mean a bunch of models which are handled as one unit when updating/adding/deleting.
Below I will describe my proposed solution to this problem of automatically updating a detached graph consisting of multiple add/delete/update changes at any point in the graph. This should work for all sorts of graphs, and allows for updating entities with associated collections and single entities.
I find its always clearer with some code so lets try an example:
Say you have a Company which has many Contacts. A contact is not defined on its own and is a One-To-Many (with required parent) record of a Company. i.e. The company is the Aggregate Root. Assume you have a detached Company graph with its Contacts attached and want to reflect the state of this graph in the database.
At present using the Entity Framework you will need to perform the updates of the contacts manually, check if each contact is new and add, check if updated and edit, check if removed then delete it from the database. Once you have to do this for a few different aggregates in a large system you start to realize there must be a better, more generic way.
Well good news is that after a few refactorings I’ve found a nice solution to this problem. The proposed extension method below handles the whole diff for you in a nice convenient package.
using (var context = new TestDbContext())
{
// Update the company and state that the company 'owns' the collection Contacts.
context.UpdateGraph(company, map => map
.OwnedCollection(p => p.Contacts)
);
context.SaveChanges();
}
Using the above code a diff will be run between the provided company graph and one retrieved from the database. The bounds of the graph are defined by the mapping which above is the company entity itself and its child Contacts. Only entities within these bounds will be included in the database diff.
The retrieval code makes use of the provided mapping configuration to get all data needed in one query at the start of the process, thus making the process quite efficient.
From this diff the algorithm will add/update/delete depending on what action needs to be performed and commit all of these changes in one batch at the end of the algorithm.
There are 2 different scenarios that this extension method must cater for. One is the situation above where you have a One-To-Many or One-To-One where the right hand side of the relationship is owned by the parent. This is defined within the mapping as an OwnedCollection/OwnedEntity.
The other scenario is that you have a Many-To-Many, or a One-To-Many and the related record exists in its own right as it not a defined part of the aggregate you are updating. That scenario is defined as an AssociatedCollection/AssociatedEntity.
The mapping configuration can handle many complex scenarios with ease. For example:
I have a company aggregate which is made of address objects and contact objects. On the contact model there is a list of accepted advertising materials (List) which are managed elsewhere in the system and not a part of the company aggregate. The list of advertisingOptions that are associated with the contact still needs to be updated however when managing the company as an aggregate.
using (var context = new TestDbContext())
{
// Update the company and state that the company 'owns' the collection Contacts.
context.UpdateGraph(company, map => map
.OwnedCollection(p => p.Contacts, with => with
.AssociatedCollection(p => p.AdvertisementOptions))
.OwnedCollection(p => p.Addresses)
);
context.SaveChanges();
}
The associated collection line tells the update method that the actual entities that are AdvertisementOptions should not be marked as changed (even if the objects provided are different from the database values. We do however want to update the relation so that it reflects the fact that the contact has the provided AdvertisementOptions and this is performed during the update.
If you would like to use this code please leave all copyright marks as they are. The code has been put up on https://github.com/refactorthis/GraphDiff.
Hope this helps you as it did our team. Please note that there are a few things that can still be improved and if you have any suggestions please let me know.
Thank you Brent for this solution. I am having a problem.
I went through your unit tests and they worked fine.
I am using Northwind database to test your extension that I would like to use if I get it to work.
I am trying to update the Shipper for the Order as below:
[TestMethod]
public void UpdateOneToOneRelationship()
{
Order order;
Shipper shipper;
using (var context = new NorthwindContext())
{
order = context.Orders.Include(“Shipper”).Single(o => o.OrderID == 10248);
shipper = context.Shippers.Single(s => s.ShipperID == 1);
}
order.Shipper = shipper;
using (var context = new NorthwindContext())
{
try
{
context.UpdateGraph(order, map => map
.AssociatedEntity(o => o.Shipper));
context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Debug.WriteLine(“Property: {0} Error: {1}”, validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
using (var context = new NorthwindContext())
{
order = context.Orders.FirstOrDefault(o => o.OrderID == 10248);
Assert.AreEqual(order.Shipper.ShipperID, 1);
}
}
And I am getting an exception
The property ‘ShipperID’ is part of the object’s key information and cannot be modified.
On the extension line 143:
context.Entry(dbvalue).CurrentValues.SetValues(newvalue);
The exception does make sense to me as the code is trying to update a key field, but I am not sure how it is working in your unit tests or projects?
Thanks for that Mustafa, It was a bug I introduced in a refactoring recently. I’ve added some tests for it and it should work fine now. Please let me know, Thanks.
Thanks a lot Brent. Your fix worked! I am doing some more tests and planning to use your extension in my repositories. I will let you know if there is anything I find or I can suggest.
You are a life saver. I having been struggling with this for many days. Shouldn’t all this be given as out-of-the-box functionality from EF? I mean this is pretty common problems to people dealing with Line Of Businness Apps.
Keep up the good work.
I was looking for such a method, and expected that it was builtin in EF (it seems to be the case for NHibernate)…
But you did a very nice job !!!
The way you are describing the relations is really a fantastic idea !
Thanks a lot!
I too was shocked to find it wasn’t supported out of the box! Since EF is open source now I might start a discussion about possibly merging this idea into the codebase.
Thanks for the comments!
What about ef 4.1? if i change the reference in the update proj to 4.1 will it still work?
Yep should not be a problem.
nuget here https://nuget.org/packages/GraphDiff
This looks promising although I haven’t had a chance to try it yet. Will it work with EF5 DbContext?
I’m having a problem when playing with GraphDiff. I have these entities (generated using the EF5 POCO template, database-first):-
public partial class Customer
{
public int Id { get; set; }
public virtual ICollection Orders { get; set; }
// other properties…
}
and
public partial class Order
{
public int Id { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection OrderLines { get; set; }
// other properties…
}
I then attempt to retrieve an existing customer and add a new order:-
using (var context = new MyTestEntities())
{
customer = context.Customers.First(o => o.Id == 1);
} // Simulate disconnect
var newOrder = new Order { Customer = customer }; // See below
customer.Orders.Add(newOrder);
using (var context = new MyTestEntities())
{
context.UpdateGraph(customer, m1 => m1.OwnedCollection(o => o.Orders));
context.SaveChanges();
}
However SaveChanges() throws the exception “Multiplicity constraint violated. The role ‘Customer’ of the relationship ‘MyTestModel.FK_Order_Customer’ has multiplicity 1 or 0..1″. It seems to be caused by me assigning the customer to the new order’s “Customer” property. If I remove this assignment then everything works. The problem is, I need to be able to pass order entities around and allow code to get at its parent Customer (via the “order.Customer” property).
Am I doing something wrong? I admit to not really understanding the difference between “owned” and “associated”, and when to use one over the other, so is this where I’m going wrong?
Hi,
Thank you for a great library.
But I’ve got a strange exception with one of models. Here is some details.
The model looks like that:
* Root entity has multiple Level1Child.
* Each Level1Child has singular Level2Child.
* Each Level2Child has multiple Level3Child.
So, I’ve tried to build the UpdateGraph function:
db.UpdateGraph(root, map => map
.OwnedCollection(r => r.Level1Children, map2 => map2
.OwnedEntity(l1 => l1.Level2Child, map3 => map3
.OwnedCollection(l2 => l2.Level3Children))));
I am getting following exception:
Property ‘System.Collections.Generic.ICollection`1[Test.Level3Child] Level3Children’ is not defined for type ‘System.Collections.Generic.IEnumerable`1[Test.Level2Child]‘
Do you have any idea – why is it like that?
Great library, saved us tons of hours. Thanks for sharing it