Entity Framework 4 - How does ObjectContext.Refresh () work?

When I call Refresh with RefreshMode.StoreWins in an entity that is in my context, and the values ​​in the database are different from what is currently in the object, my object must update the current values ​​with respect to the database data, even if my entityState has not changed?

When editing an object, we create a new context and create a new class, ourClass (classId), which loads the current entity of our class type from the database. Making changes to our class and calling SaveChanges correctly stores the values ​​in the database. After returning to the calling view model, we call Refresh in the existing context using RefreshMode.StoreWins, but this does not update the entity values ​​in this context, even though the values ​​in the database were correctly updated using SSMS. Any ideas what I should learn to solve this problem?

EDIT: A simple example of how we do things:

var context1 = new Model1(); LoadContext(); //loads all the data from the database and adds them to the context var context2 = new Model1(); var SelectedObject = context1.Table1.First(); OurObject selectedObjectForEdit = new OurObject(SelectedObject.ObjectId); context2.Table1.Add(selectedObjectForEdit); selectedObjectForEdit.Name = "new name"; context2.SaveChanges(); context1.Refresh(RefreshMode.StoreWins, SelectedObject); 
+4
source share
1 answer

You must pay attention to the context in order to update an object that is in a self context in no other context. Run the object with a different context manually, run without any exceptions and do not change anything even with SoreWins.

 var context1 = new Model1(); var context2 = new Model1(); context1.Table1.First().Caption = "a"; var entity = context2.Table1.First(); context1.SaveChanges(); //below code run without exception but any change not affected context1.Refresh(RefreshMode.StoreWins, entity); 

context1 force update an object included in context2.

0
source

Source: https://habr.com/ru/post/1382171/


All Articles