本文介绍了强制LinqToSql提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制LinqToSql DataContext将属性视为脏属性并将其提交给数据库?

How can I force LinqToSql DataContext to consider a property dirty and submit it to the database?

在我的情况下,该属性是XElement,该属性已被修改,但DataContext不会捕获它,因为实际引用保持不变.

in my case the property is XElement, which gets modified, but DataContext doesn't catch it, since the actual reference stays the same.

我想我可以尝试分配属性null或新的XElement,然后将其分配回原始XElement.这将是最好的解决方案吗?

I guess I could try to assign property null or new XElement and then assign it back to the original XElement. would this be the best solution?

修改XElement子级后,我正在调用SubmitChanges.

I am calling SubmitChanges after XElement children are modified.

推荐答案

我还没有找到一种强制实体上的属性变脏的方法.从LinqToSql代码的挖掘来看,似乎是跟踪了旧值,然后将其与新值进行了比较,因此通常必须使用Attach或必须使用当前对象来创建新引用.

I haven't found a way to force property on entity to become dirty. from digging in LinqToSql code it appears that old value is tracked and then compared to the new value, so in general either Attach must be used or current object must be clonned to create new reference.

我通过自定义表类来解决XElement的问题.更改XElement时,我只是基于旧元素创建新的XElement:

I worked around my problem with XElement by customizing my table class. when XElement is changed, I simply create new XElement based on the old element:

partial class Table
{
    partial void OnLoaded()
    {
        if( XmlData != null )
        {
            XmlData.Changed += this.XmlData_Changed;
        }
    }

    private void XmlData_Changed( object sender, XObjectChangeEventArgs e )
    {
        this.ModifiedCardData = new XElement( this.XmlData );
    }
}

这篇关于强制LinqToSql提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:46