Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 913147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:30:21+00:00 2026-05-15T17:30:21+00:00

Self tracking entities. Awesome. Except when you do something like return Db.Users; none of

  • 0

Self tracking entities. Awesome.

Except when you do something like

return Db.Users;

none of the self-tracking entities are tracking (until, possibly, they are deserialized).

Fine. So we have to recognize that there is a possibility that an entity returning to us does not have tracking enabled.

Now what???

Things I have tried

For the 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 all fail:

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

Nor about anything else I can figure to throw at it, other than

  1. Getting the original entity from the database
  2. Comparing each property by hand
  3. Updating properties as needed

What, exactly, am I supposed to do with self-tracking entities that aren’t tracking themselves??


Small update:

Blindly marking the entity as modified works, however this seems a bit smelly. Is it the best we can do in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:30:21+00:00Added an answer on May 15, 2026 at 5:30 pm

    scenario 1

    Here are some recommended practices to follow.
    When you are using STE in WCF scenario, you should rely on the change tracker 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 the server, the recommended practice is to create a method on the partial class for objectcontext called EnableChangeTracking. The method would query for entities that are in unchanged state that implements IObjectWithChangeTracker and turns on change tracking so something like this

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

    now try to save the user entity from a different context from which it was originally retrieved from

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

    scenario 3
    if on the server side you are connected to the same object context from which you retrieved the user entity from, then you use STE as simple poco object like 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 entity is retrieved from a different context then the context u will use it to save then here is another option where u mark the entity as modified and not care what got modified.

    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 entity is retrieved from a different context then the context u will use it to save then here is another option where u retrieve the original entity.

    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 few scenarios.
    http://weblogs.asp.net/zeeshanhirani/archive/2010/03/30/modifying-self-tracking-entity-on-the-server.aspx

    I extensively cover these concepts in my Entity Framework 4.0 recipes book with lots of scenarios..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGrid that binding an ObservableCollection of self tracking entities Users can
1) Since self-tracking entities do not support lazy loading, are they intended to be
I've been reading about self-tracking entities in .net and how they can be generated
I'm using Self Tracking Entities that implements IObjectWithChangeTracker with the last Entity Framework RC
I'm using EF4.x Self Tracking Entities in my project and am trying to achieve
I have a Silverlight application that I use the Beta2 T4 Self tracking entities
I've followed this walkthrough to create a wcf+ef+self-tracking entities project. Walkthrough: Serialize Self-Tracking Entities
My application is using Self-Tracking Entities & I get my data from a WCF
I am using self tracking entities and have moved the entity classes to another
I want to serialize an Entity Framework Self-Tracking Entities full object graph (parent +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.