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