带有 XML 数据库字段的 Linq-to-SQL -- 为什么这样做? [英] Linq-to-SQL With XML Database Fields -- Why does this work?

查看:38
本文介绍了带有 XML 数据库字段的 Linq-to-SQL -- 为什么这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景:我有一个数据库,我想使用 linq-to-sql 通过 C# 应用程序进行更新.此表中的一列具有 XML 数据类型.

Some background: I have an database that I want to use linq-to-sql to update through a C# application. One of the columns in this table has an XML datatype.

该表中的所有其他列(不是 XML 数据类型)更新得很好,但是当我对 XML 字段进行更改时,程序执行(看似)正确,但该字段始终保留其原始数据运行 SubmitChanges() 后的值.

Every other column in that table (that isn't of an XML datatype) updates perfectly fine, but when I went to make changes to the XML field, the program executes (seemingly) correctly, but the field always retains its original value after I run SubmitChanges().

我环顾互联网,在 Microsoft Connect 上发现了一些诊断类似问题的帖子,最后偶然发现了解决方案 这里:

I looked around the internet and found a few posts on Microsoft Connect diagnosing similar problems and finally stumbled on the solution here:

要强制更新 XML 字段,这样做是行不通的:

To force XML field update this won't do:

XElement tmp = MyLinqObject.XmlField;
MyLinqObject.XmlField = null;
MyLinqObject.XmlField = tmp;

而不是强制 LINQ 更新 XML 列分配一个克隆对象:

Instead of that to force LINQ to update XML column assign a cloned object:

MyLinqObject.XmlField = new XElement (MyLinqObject.XmlField);

我可以确认这确实有效,但我不确定为什么.我唯一的猜测是 XmlField 属性在堆上具有某种唯一标识符,并且通过进行克隆,您已为其分配了一个新的唯一标识符.当 Linq 然后生成查询时,它甚至不会尝试查看该字段是否已更新,因为它有一个新的 id,它只是将值写入数据库.但是,我只是推测,希望其他人可以更好地了解幕后发生的事情.

I can confirm that this does indeed seem to work, but I'm not exactly sure why. My only guess is that the XmlField Property has some sort of unique identifier on the heap and that by making a clone, you've assigned it a new unique identifier. When Linq then generates the query, it doesn't even attempt to see if the field has been updated, since it has a new id, it simply write the value to the database. But, I'm simply speculating and hope that someone else can provide a better understanding of what is going on behind the scenes.

编辑:为了解决 Jon 的帖子,问题的原因(如 MS Connect 站点上所述)是XML 字段未更新,因为 Linq-to-SQL 未更新"t 处理 XElement.Changed 事件".

EDIT: To address Jon's post, the reason for the issue (as it is explained on the MS Connect site) is that "the XML field does not update because Linq-to-SQL doesn't handle the XElement.Changed event".

对于我的实现,有效的代码最终看起来像这样:

For my implementation, the code that works ends up looking something like this:

MyXElementProperty.SetElementValue("Author", author);

MyXElementProperty = new XElement(MyXElementProperty);

作为参考(给发现这个问题的其他人),以下也适用:

For reference (to anyone else that finds this question), the following also works:

MyXElementProperty = new XElement(MyXElementProperty);

MyXElementProperty.SetElementValue("Author", author);

推荐答案

当您将其设为新的 XElement 时,您正在创建一个不同的对象.为了检测陈旧",它可能使用引用相等性,这显然会将其视为新值.

When you make it a new XElement, you're making a different object. It's possible that to detect "staleness" it's using reference equality, which would obviously treat this as a new value.

您实际上在什么时候对元素进行了更改?我希望 LINQ to SQL 具有原始值的缓存副本,然后将其与新值进行比较.如果它只是通过复制引用来获取缓存副本",那么无论您对该对象做什么,它都会认为两者是相等的.相反,如果您创建一个 new 元素,然后更改 那个,那么旧对象仍将具有旧值,因此比较将了解您进行了更改.有意义吗?

At what point are you actually making changes to the element? I would expect LINQ to SQL to have a cached copy of the original value, and then compare that with the new value. If it's taking that "cached copy" by just copying the reference, then whatever you do to that object, it will always think the two are equal. If instead you create a new element, and then change that, then the old object will still have the old value, so the comparison will understand that you've made changes. Does that make sense?

这篇关于带有 XML 数据库字段的 Linq-to-SQL -- 为什么这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆