Is SubmitChanges() not Working in LinQ ?

Some times this type of problem comes especially when you are working with N-tier application. I got this problem when I was trying to made a change in database and I wrote

(Here Domain is a class and table in database and permission is a field of this table)

public static void UpdateXmlByDomainID(int domainID, Settings permissions)

        {

MiniSitesDataContext DataContext = MSD.GetDataContext();

 

XElement xml = Util.GetXmlInXElement(permissions);

 

            Domain updatedDomain = GetDomainByID(domainID);

           

updatedDomain.Permissionsxml = xml;          

            DataContext.SubmitChanges(); 

 

        

        }

Where GetDomainByID(domainID) is a function to fetch Domain but by this method I was unable to change then I a made a change.

Hurrah! It’s working

public static void UpdateXmlByDomainID(int domainID, Settings permissions)

        {

            MiniSitesDataContext DataContext = MSD.GetDataContext();

                        

XElement xml = Util.GetXmlInXElement(permissions);

 

            Domain updatedDomain = new Domain();

           

updatedDomain = DataContext.Domains.Single(d => d.DomainID == domainID);

           

updatedDomain.Permissionsxml = xml;          

            DataContext.SubmitChanges();          

                    

        }

Actually problem is this, LinQ put DataContext on track so this type of problems arising. In first one DataContext was not identifying that this domain is related to it but in second one we are fetching object from datacontext so when we submit changes it make change in database.

Note: This example is not related to only xml functions. Applicable any where. If your problem is still unsolved you are free to ask, may be I can help you 🙂 

Problem in linQ Updates – Object-tracking and Caching

As I told I am working on linQ and daily I get some interesting problems. I feel LinQ has some issues and today I made an application to research. I am surprised because I found a major issue which I am going to discuss.

The problem is related to database update. After fetching an object from database if you are assigning it some other value and then you submit your changes or not but in application it will show you have changed the value. If you submit those changes then it will reflect in database. Irrespective of database changes, it shows in application that it’s changed.
Even I fetched data again using different object but it shows changed value in spite of the fact that I didn’t submit changes.

I want to show an example of this,

In database I have a table by name ‘Contact’

namespace WebApplication1

{

    public partial class _Default : System.Web.UI.Page

    {

        DataClasses3DataContext db = new DataClasses3DataContext();

        Contact c, d;

        protected void Page_Load(object sender, EventArgs e)

        {   

            // feching data for contactid 7

            c = db.Contacts.Single(a => a.ContactID == 7);           

 

            TextBox1.Text = c.ContactID.ToString();

            TextBox2.Text = c.Name;

            TextBox3.Text = c.Organization; 

           

        }

    }

}

 

Result  

 

 

 

 

 

Again I made some change in code and write a button event also

 

namespace WebApplication1

{

    public partial class _Default : System.Web.UI.Page

    {

        DataClasses3DataContext db = new DataClasses3DataContext();

        Contact c, d;

        protected void Page_Load(object sender, EventArgs e)

        {   

            // feching data for contactid 7

            c = db.Contacts.Single(a => a.ContactID == 7);

 

            c.Name = “changed”; // assigning the value

 

            TextBox1.Text = c.ContactID.ToString();

            TextBox2.Text = c.Name; // now it have new value “changed”

            TextBox3.Text = c.Organization;           

 

 

            c = db.Contacts.Single(a => a.ContactID == 7);         

          

 

            TextBox1.Text = c.ContactID.ToString();

            TextBox2.Text = c.Name; // now it shoulda have value from database which is larry

            TextBox3.Text = c.Organization;

          

           

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

 

            d = db.Contacts.SingleOrDefault(a => a.ContactID == 7);

                     

            TextBox6.Text = d.ContactID.ToString();

            TextBox7.Text = d.Name; // now it should have value from database which is larry

            TextBox8.Text = d.Organization;       

 

        }

 

 

 

    }

}

 And the result is

 

 

Remember I didn’t use any where SubmitChanges().

So now what should we do??

Answer is very simple don’t assign value to object member, if you haven’t made any change.
If it’s conditional then first check, and if change is needed then assign.

Actually the reason of the problem is tracking and caching of objects, so there is one more option available for that and you can disable object tracking by a single line of code. But it should be written just after creating DataContext.

DataClasses3DataContext db = new DataClasses3DataContext();

db.ObjectTrackingEnabled = false;

and code should be written before LinQ-query.