Reputation: 150188
I'm implementing a Producer / Consumer pattern:
Producer
Consumer (Multiple threads)
The Issue
The objects are materialized using Entity Framework Code First using a context associated with the producer thread.
The consumer threads require their own context.
Is it possible to move the materialized objects (each object is really an object graph) from the producer context to the consumer context, or must I rematerialize the object in the consumer's context in order to update it there? How can I do the move?
Upvotes: 2
Views: 392
Reputation: 34299
I think what you want is the following:
Entity framework allows you to re-attach an object to the contexts tracking graph and treat it as the current state of the object in the database. This means it will only update the properties you touch in your update block.
Ie you want to follow this update without prior select path for updates:
using (var context = new MyContext())
{
var dummy = new Test{Id= 1};
context.Tests.Attach(dummy);
dummy.Something = "Hello World";
context.SaveChanges();
}
Whats important to note about this method is that you can only attach an entity (defined by a unique key) to the tracking graph once. So its really important to do this on an isolated context or to carefully manage the attach call.
Upvotes: 2