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 748437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:19:13+00:00 2026-05-14T14:19:13+00:00

I started messing with EF 4.0 because I am curious about the POCO possibilities…

  • 0

I started messing with EF 4.0 because I am curious about the POCO possibilities… I wanted to simulate disconnected web environment and wrote the following code to simulate this:

  1. Save a test object in the database.
  2. Retrieve the test object
  3. Dispose of the DataContext associated with the test object I used to retrieve it
  4. Update the test object
  5. Create a new data context and persist the changes on the test object that are automatically tracked within the DynamicProxy generated against my POCO object.

The problem is that when I call dataContext.SaveChanges in the Test method above, the updates are not applied. The testStore entity shows a status of “Modified” when I check its EntityStateTracker, but it is no longer modified when I view it within the new dataContext’s Stores property. I would have thought that calling the Attach method on the new dataContext would also bring the object’s “Modified” state over, but that appears to not be the case. Is there something I am missing? I am definitely working with self-tracking POCOs using DynamicProxies.

private static void SaveTestStore(string storeName = "TestStore")
{
  using (var context = new DataContext())
  {
    Store newStore = context.Stores.CreateObject();
    newStore.Name = storeName;
    context.Stores.AddObject(newStore);
    context.SaveChanges();
  }
}

private static Store GetStore(string storeName = "TestStore")
{
  using (var context = new DataContext())
  {
    return (from store in context.Stores
            where store.Name == storeName
            select store).SingleOrDefault();
  }
}

[Test]
public void Test_Store_Update_Using_Different_DataContext()
{
  SaveTestStore();
  Store testStore = GetStore();
  testStore.Name = "Updated";      

  using (var dataContext = new DataContext())
  {
    dataContext.Stores.Attach(testStore);
    dataContext.SaveChanges(SaveOptions.DetectChangesBeforeSave);        
  }

  Store updatedStore = GetStore("Updated");
  Assert.IsNotNull(updatedStore);
}
  • 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-14T14:19:14+00:00Added an answer on May 14, 2026 at 2:19 pm

    As you stated later, you were using the POCO generator, not the self-tracking entities generator.

    I’ve tried it as well, and became quite perplexed. It seems that the proxy classes don’t quite work as expected, and there might be a bug. Then again. none of the examples on MSDN try something like this, and when they reference updates in different tiers of an app (something like we’re doing here) they use self-tracking entities, not POCO proxies.

    I’m not sure how these proxies work, but they do seem to store some kind of state (I managed to find the “Modified” state inside the private properties). But it seems that this property is COMPLETELY ignored. When you attach a property to a context, the context adds an entry to the ObjectStateManager, and it stores further state updates in there. At this point if you make a change – it will be registered, and applied.

    The problem is that when you .Attach an entity – the Modified state from the proxy is not transferred to the state manager inside the context. Furthermore, if you use context.Refresh() the updates are override, and forgotten! Even if you pass RefreshMode.ClientWins into it. I tried setting the object state’s state property to modified, but it was overridden anyway, and the original settings were restored..

    It seems that there’s a bug in the EF right not, and the only way to do this would be to use something like this:

    using (var db = new Entities())
    {
        var newUser = (from u in db.Users
                        where u.Id == user.Id
                        select u).SingleOrDefault();
        db.Users.ApplyCurrentValues(user);
        db.SaveChanges();
    }
    

    One more thing here

    Entitity Framework: Change tracking in SOA with POCO approach

    It seems that POCO just doesn’t support the approach you’re looking for, and as I expected the self-tracking entities were created to tackle the situation you were testing, while POCO’s proxies track changes only within the context they created.. Or so it seems…

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

Sidebar

Related Questions

So I started messing around with gettext but I'm still puzzled about certain things,
I started messing around with Git today. I think I've done something wrong because
I've just started messing around with php recently and I was testing my new
I have finally started messing around with creating some apps that work with RESTful
For a pet project I started to fiddle with ANTLR. After following some tutorials
My test started failing because I added a debugger after an XML expected block.
Yesterday, I started here asking a question about my own project, and when I
I started win32 programming for fun because I like complex things and I like
I've recently started using GSON library for deserializing JSON that comes from web service
Very confused over this. I've just started learning about pointers and have now decided

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.