反序列化实体框架对象在我的数据库中创建重复记录

发布于 2024-11-26 12:29:05 字数 1387 浏览 10 评论 0原文

我有一些从数据库获取的 .NET 4 实体框架对象,然后将它们序列化为 XML。然后我退出 WPF 应用程序(清除内存)。然后我重新启动 WPF 应用程序,并将它们读回(反序列化)到 List<> 中。但切勿将它们附加到任何 EF 上下文。当我在对象上下文上调用 SaveChanges() 时,它会创建重复的记录,但我从未将反序列化的记录附加到上下文,因此我不确定为什么新上下文要创建记录的副本。这是否与自我跟踪实体有关 http://msdn.microsoft. com/en-us/library/ff407090.aspx

这是评论...

启动应用程序

查询对象到 ObjectSet.ToList() _cachedRates

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

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

序列化为 XML

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

关闭应用程序

...{稍后}...

再次启动应用程序

从 XML 文件加载对象,对象永远不会附加()-ed 或 AddObject()-ed 到任何上下文。

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();
}

如果用户按下“保存”按钮,它会在上下文中调用 .SaveChanges()

问题:我的表中现在的匹配行数是原来的两倍

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2024-12-03 12:29:05

由于您将对象从一个上下文序列化,然后将它们反序列化回具有不同上下文的对象,因此您将看到这种行为。一旦您从当前上下文中分离了一个对象/项目,如果您打开另一个上下文并且不将其重新附加到当前上下文,则上下文将认为它是一个新对象。在这种情况下,一种选择可能是使用您已缓存的费率 ID,在上下文中调用 .SaveChanges() 方法之前从上下文中提取这些费率,然后使用上下文中的这些费率,而不是使用缓存的对象。我最终需要在当前的 MVC 应用程序中执行与此非常类似的操作,因为我通过 JSON 发送复杂模型,如果我希望子集合正确保存,我需要复制父对象并将其附加到上下文空子集合。然后,我直接从上下文查询 JSON 对象的每个属性中的子对象,并在保存之前将上下文中的子对象分配给附加对象的子对象。这似乎对我有用。如果我尝试将反序列化的 JSON 对象与子对象一起保存,我最终会在数据库中得到重复的子行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文