527886 (1) [Avatar] Offline
#1
Dear Reader/John,

I'm dealing with confusion about when i would be dealing with a disconnected state update.
when do 2 seperate instances of the dbcontext occur?
Is it when you have 2 seperate controllers with 2 seperate DbContextes from the constructor
dealing with the same entity?

In that case:
If i Create the following inside the controller
UpdateExampleService exupdate = new UpdateExampleService(I have to pass in this instance of the db context here),
And in the other controller i would pass in a different instance of the dbcontext...

I totally do not understand when a disconnected state update occurs
and how to handle this situation from an asp.net core web app kind of view...

Excuse my english...
Kind Regards,
Noah (ambitious reader)
Jon P Smith (20) [Avatar] Offline
#2
Hi Noah,

The disconnected state happens in a web application because each HTTP request runs on it own. Typically for an update you have:
1. A HTTP request that shows a screen with the item to be changed. Once the screen is shown the HTTP request is finished.
2. Then user then enters the new data clicks the Update/Submit button, which causes a new HTTP request to be sent back to the web application.

A good diagram for this is figure 5.6 on page 130 of the latest MEAP.

Because everything is lost between the two HTTP requests two things need to happen to update something:
a. The first HTTP request must pass any data needed in the second HTTP request, like the primary key and the new data.
b. You have to re-read the entity (see listing 3.13 its the book) and then update it with the data returned from the first HTTP request.

On your question about controllers, I cover how ASP.NET Core makes sure all the controllers have the same instance of the application's DbContext, via the Scoped lifetime. See figure 5.3 on page 123.

I hope this helps, and I will try an add a diagram in chapter 3 to make this clearer. Thanks for pointing this out to me.