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.