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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:14:37+00:00 2026-05-24T00:14:37+00:00

I have some .NET 4 entity framework objects that I get from the DB,

  • 0

I have some .NET 4 entity framework objects that I get from the DB, then I serialize them to XML. Then I quit the WPF app (clear out the memory). Then I restart the WPF app and I read them (deserialize) back into a List<> but never attach them to any EF context. When I call SaveChanges() on my object context, it cretes duplicate records, but I never attached the deserialized to the context so I’m not sure why the new context is creating copies of the records. Does this have something to do with self-tracking entities http://msdn.microsoft.com/en-us/library/ff407090.aspx?

Here’s a review…

Start app

Query objects into an ObjectSet.ToList() _cachedRates

IQueryable<Rate> query = DB.EF.Rates.Where({some predicates});

if (query != null && query.Count() > 0)
    _cachedRates = query.ToList();

Serialize to XML

XmlSerializer serializer = new XmlSerializer(_cachedRates.GetType());
TextWriter textWriter = new StreamWriter(saveDialog.FileName);
serializer.Serialize(textWriter, _cachedRates);
textWriter.Close();

Close the app

…{later}…

Start the app again

Load the objects from an XML file, the objects are never Attach()-ed or AddObject()-ed to any context.

if (openDialog.ShowDialog().Value)
{
    _cachedRates = null;

    XmlSerializer deserializer = new XmlSerializer(typeof(List<Rate>));
    TextReader textReader = new StreamReader(openDialog.FileName);
    _cachedRates = (List<Rate>)deserializer.Deserialize(textReader);
    textReader.Close();
}

If the user presses the “Save” button it calls .SaveChanges() on a context

PROBLEM: I now have twice as many matching rows in my table

  • 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-24T00:14:38+00:00Added an answer on May 24, 2026 at 12:14 am

    Since you are serializing your objects away from one context and then deserializing them back into object with a different context, you will see this behavior. Once you detach an object/item from the current context, if you open another context and do not reattach it to the current context, the context will think that it is a new object. One option in this scenario might be to us the ids of the rates that you have cached to pull those rates from the context prior to the .SaveChanges() method being called on the context and then working with those rates from the context instead of the cached object. I end up needing to do something very similar to this in my current MVC application as I am sending in a complex model via JSON and if I want the child collections to save properly, I need to copy and attach the parent object to the context with empty child collections. Then I query the child objects in each property on the JSON object directly from the context and assign the ones from the context to the child objects of the attached object before saving. This seems to work for me. If I try to just save the deserialized JSON object with the children, I end up getting duplicate child rows in my database.

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

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
I have never used Entity Framework in a project before so I am not
I am very new to ADo.net entity framework and i am getting the following
I'm writing an ASP.net MVC3 application using Entity Framework Code First and SqlCe4. I
i have a input tag which is non editable, but some times i need
I am attempting to pull some information from my tnsnames file using regex. I
I'm in the process of porting some code from Linux to Mac OS X.
I would like to get a sum from a column, with and without a
I am a .NET Developer and as part of a refactoring project, I have
I'm not sure if this is possible with the Entity Framework but I'm duplicating

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.