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

  • Home
  • SEARCH
  • 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 some .net apps running that I need to monitor for example, then
Now that I have built a database. Visual Studio 2008 SP1’s ADO.NET Entity Framework
I have built an ASP.NET MVC web application that uses Entity Framework. To provide
I have some applications (some native, some .NET) which use manifest files so that
I have some ASP.NET page and webservice WebMethod() methods that I'd like to add
We have built some .NET controls, and we are using them as COM controls
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
Im currently looking at migrating from fluent nHibernate to ADO.Net Entity Framework 4. I
Using ASP.NET MVC and Entity Framework, I encounter some attach/detach errors when I need
I have one solution with three projects. DomainModel (C# Library with ADO.NET Entity Framework)

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.