tags:

views:

53

answers:

3

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

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

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?

I am calling SubmitChanges after XElement children are modified.

A: 

I can't tell from your question, but are you calling SubmitChanges() after you modify the property? If not, that might be your solution.

This ScottGu post shows examples of inserts and updates using LINQ to SQL.

Jonathan S.
A: 

You may need to use the Attach(object, true) where you are telling the DataContext that the version you are passing in is what you want saved. Then you can call SubmitChanges() from there.

Andrew Siemer
But I don't want to force submit of the whole entity. just of the property that is modified.
Ornus
A: 

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.

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