How can I track tracking objects without tracking?

Self Tracking Objects. Tall.

Except when you do something like

return Db.Users; 

None of the objects of self-monitoring are monitored (until, perhaps, they are deserialized).

Fine Therefore, we must recognize that there is a possibility that the object returning to us does not have tracking.

Now what ???

Things i tried

For a given method body:

 using (var db = new Database()) { if (update.ChangeTracker.ChangeTrackingEnabled) db.Configurations.ApplyChanges(update); else FigureItOut(update, db); db.SaveChanges(); update.AcceptChanges(); } 

The following implementations of FigureItOut :

 db.Configurations.Attach(update); db.DetectChanges(); 

Nor

 db.Configurations.Attach(update); db.Configurations.ApplyCurrentValues(update); 

Nor

 db.Configurations.Attach(update); db.Configurations.ApplyOriginalValues(update); 

Nor

 db.Configurations.Attach(update); db.Configurations.ApplyChanges(update 

I can’t understand anything else to throw at him except

  • Getting the source object from the database
  • Manually comparing each property
  • Update properties as needed

What, in fact, should I do with self-observation that does not track itself?


Small update:

Blindly marking an object as modified, but it seems a little smelly. Is this the best we can do in this case?

+4
source share
1 answer

script 1

The following are some recommended practices. When you use STE in a WCF script, you must rely on the change tracking that STE implements, so on the server side you do the following.

 db.Users.ApplyChanges(user); db.SaveChanges(); 

Scenario 2 However, if you are on a server, the recommended practice is to create a method for a partial class for an objectcontext called EnableChangeTracking. This method will query for objects that are in a constant state that implements IObjectWithChangeTracker and includes change tracking, something like this

 user = db.users.first(u => u.userid == 1); db.EnableChangeTracking(); 

now try saving the user object from a different context from which it was originally extracted from

 db2.users.ApplyChanges(user); db2.SaveChanges(); 

scenario 3 if on the server side you are connected to the same object from which you extracted the user object, then you use STE as a simple poco object, as shown below.

 user = db.users.first(u => u.userid == 1); user.LastName = "XYZ"; db.DetectChanges(); //no need for it cuz Savechanges implicitly calls this. db.SaveChanges(); 

scenario 4 if the user object is retrieved from another context, then the context u will use it to save, then there is another option where u puts the object as changed and do not care about what was changed.

 user = db.users.first(u => u.userid == 1); var db2 = new ObjectContext(); user.LastName = "XYZ"; db2.Users.Attach(user); // i prefer this option.. db2.ObjectStateManager.ChangeObjectState(user,EntityState.Modified); db2.SaveChanges(); // updates all columns 

scenario 5 if the user object is retrieved from another context, then the context u will use it to save, then there is another option when u retrieves the original object.

 user = db.users.first(u => u.userid == 1); user.lastName ="XYZ"; var db2 = new ObjectContext(); db2.Users.First(u => u.userid == user.userid); db2.users.ApplyCurrentValues(user); db2.SaveChanges(); 

Here is a blog post that describes several scenarios. http://weblogs.asp.net/zeeshanhirani/archive/2010/03/30/modifying-self-tracking-entity-on-the-server.aspx

I cover these concepts extensively in my Entity Framework 4.0 recipe book with lots of scripting.

+6
source

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


All Articles