Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving a ObjectContext depended object after two requests (EF/MVC 3)
    primarykey
    data
    text
    <p>Using EntityFramework, I am trying the following in combination with MVC 3.</p> <p>I have a <code>ObjectContext</code>, which is being initialized at the start of every request, and disposed at the end of the request. Much like a simple <code>UnitOfWork</code> implementation. Now think of having a <code>Order</code> class (EF generated), which is going to be initialised. I pass the <code>ObjectContext</code> to be used trough a overloaded constructor.</p> <pre><code>var order = new Order(myObjectContext); </code></pre> <p>Then inside the <code>Order</code> class this instance is kept (private field). After I set some properties of this <code>Order</code> instance (using the same <code>ObjectContext</code>). I want to put this instance in the Users' session because a second webpage is needed to populate some information before the order is ready to be saved.</p> <p>This is were I'm stuck, after the user submits the second webpage I retrieve the <code>Order</code> object from the session, set some more properties and hit <code>.SaveChanges()</code> on the <code>ObjectContext</code> references by the <code>Order</code> object. So the one that was made in the first request. I think this is required because during the first request (when the <code>Order</code> was created) some other entities are being added to one of the orders collection. So I have to use the same context again. The problem is that after hitting the <code>.SaveChanges()</code> the method returns no error or whatsoever, but the data is not saved in the database. Even the identity value does not change from zero. Nothing is being committed.</p> <p>When I first try to add the object to the collection:</p> <pre><code>this._context.Order.AddObject(this); this._context.SaveChanges(); </code></pre> <p>Please note this code is within a method, placed in a partial Order class. This time, at the <code>.AddObject()</code> I get the message:</p> <p><code>An entity object cannot be referenced by multiple instances of IEntityChangeTracker.</code></p> <p>I tried to detach the <code>Order</code> object from it's original context and reattach it to the context made in the second request but of course this is not working since one of the <code>Order</code> properties is a collection of other entities, retrieved and added in the first request, thus with the first <code>ObjectContext</code>.</p> <p>Probably I could overcome this issue by calling <code>.SaveChanges()</code> at the end of the first request. Then in the next request I would have to pull the order from the db with that request's <code>ObjectContext</code>. The thing is, I do not want to save the <code>Order</code> before the end of the second request.</p> <p>To me it looks like the <code>ObjectContext</code> which is kept with the <code>Order</code> instance in the session needs a 'connection-refresh'?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload