I read record from database and dispose ObjectContext associated with it. Later on when consumer finished with record I need to update it in db. How can do it with new context?
Here is how I read records from db and put them into BlockingCollection:
var products = from r in context_.Products
orderby r.id
select r;
List<Product> pList = products.Take(10).ToList();
Now how to update those record in database with new context? Thanks!
I tried the following, but failed:
SMEntities context = new SMEntities();
Product p = outPipe.GetConsumingEnumerable().First();
outContext.Products.Attach(p);
outContext.SaveChanges();
It returns with an Exception: object cannot be referenced by multiple instances of IEntityChangeTracker
UPDATE (explanation why I do not want to use the same context): Product record are consumed by many tasks in different threads and I won't call context.SaveChanges in producer thread, as some of the record in threads may be in the middle of setting changes to the Product record. What will happen in this case?