Last post Mar 20, 2015 09:35 AM by sudip_inn
Mar 19, 2015 04:41 AM|sudip_inn|LINK
i hard that EF works in connected and disconnected way. so i got a two code one is for connected and other one for disconnected.
first image one for connected approach
second image one for disconnected approach
please tell me why first one is calling connected approach and second one is disconnected approach. what is the difference between the two code above.
how do i determine which one is connected approach and which one is disconnected?
please discuss in details. thanks
Mar 19, 2015 05:54 AM|Mikesdotnetting|LINK
A connected scenario refers to a scenario where the objects you are working with are connected to or being tracked by the context all the time you are working with them. This is more likely to apply to WPF applications. Disconnected scenarios are more common
in web applications, where the context is newed up per request, and the objects are passed to it from an external source. In these scenarios, you have to manually set the state of an entity so that the context knows whether it is to be added, updated etc.
Mar 19, 2015 06:46 AM|sudip_inn|LINK
i was just simply trying to know why the first code is consider connected approach and why second one is consider as disconnected. please for what special reason first code is consider connected approach ? please discuss
Mar 19, 2015 06:52 AM|Mikesdotnetting|LINK
In the first example, the entities are always attached/connected to the context. In the second example, the entity is introduced to the context from an external source, so initially, it was disconnected.
Mar 20, 2015 05:00 AM|Zhi Lv - MSFT|LINK
With reference to
this link, I found this:
Connected Scenario:Connected scenario is when an entity is retrieved from the database and modified in the same context.
Disconnected Scenario:Disconnected scenario is when an entity is retrieved from the database and modified in the different context. Disconnected scenario is complex because context doesn’t know anything about modified entity so you have to tell
to ObjectContext that what has changed in entity.
Besides, you could also refer to this article:
Connected And Disconnected Scenarios in Entity Framework
Mar 20, 2015 05:02 AM|sudip_inn|LINK
very sorry mike that you explain so briefly that i could get the catch. what u r trying to say attached ??
it would be nice if you pin point the line for which people say connected / disconnected approach. if possible come with small sample code with explanation. thanks
Mar 20, 2015 05:31 AM|Mikesdotnetting|LINK
There isn't one line in particular that determines whether you are working in a connected or disconnected manner. Your code works just fine as an example. In the first example, you only ever reference the entities via the context. So they are always attached/connected
to the context right from the moment when you first reference them, through when you are modifying them and when you persist those changes by calling SaveChanges. The context is able to track them and their
State all the way through the process because they are connected.
Entity State is one of a number of values: Added, Modified, Deleted, Attached, Unchanged (https://msdn.microsoft.com/en-us/library/system.data.entitystate%28v=vs.110%29.aspx)
In the second example, you introduce an entity to the context from an outside source (via the parameter to the UpdateEmployee method). The context doesn't know anything about the State of this entity because it wasn't initially connected to the context.
It was disconnected. You connected it when you call the Attach method in Line 2. Because it was disconnected, you also have to tell the context what the State of the entity is, so in line 3, you explicitly set the State to Modified.
Mar 20, 2015 09:35 AM|sudip_inn|LINK
i like to know that when this below line will execute then a connection will be opened or established to database
or connection will be open when context.SaveChange() will execute ?
var container = new CotainerEntites();