Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Most efficient way to update with LINQ to SQL

4.25/5 (5 votes)
1 Apr 2012CPOL1 min read 47.9K  
Most efficient way to update with LINQ to SQL

This article is about the question that I posted on the one of the community site and the best-est answer I found for that question. The question is all about LINQ TO SQL  to update data. Information in the article is helpful to the beginner level developer who might have question about updating the records in LINQ to SQL ORM. 

Here I have question about updating the Employee data which are fetch by me to display the user and than updating employee information , saving data back to database.

Question 

Can I update my employee record as given in the function below or do I have to make a query of the employee collection first and then update the data?  

public int updateEmployee(App3_EMPLOYEE employee)
      {
          DBContextDataContext db = new DBContextDataContext();
          db.App3_EMPLOYEEs.Attach(employee);
          db.SubmitChanges();
          return employee.PKEY;
      }
Or do I have to do the following?
public int updateEmployee(App3_EMPLOYEE employee)
    {
        DBContextDataContext db = new DBContextDataContext();
        App3_EMPLOYEE emp = db.App3_EMPLOYEEs.Single(e => e.PKEY == employee.PKEY);
        db.App3_EMPLOYEEs.Attach(employee,emp);
        db.SubmitChanges();
        return employee.PKEY;
    }
But I don't want to use the second option. Is there any efficient way to update data? I am getting this error by using both ways:
An attempt has been made to Attach or Add an entity that is not new, 
perhaps having been loaded from another DataContext.  
This is not supported.

I googled to get answer of the question and to resolve the issue and get following answer from the different sites and sources. So based on that I got following 5 way to do data update. 

Answer 

I find following work around to this problem :

1) fetch and update entity (i am going to use this way because it ok for me )

public int updateEmployee(App3_EMPLOYEE employee)
    {
        AppEmployeeDataContext db = new AppEmployeeDataContext();
        App3_EMPLOYEE emp = db.App3_EMPLOYEEs.Single(e => e.PKEY == employee.PKEY);
        emp.FIRSTNAME = employee.FIRSTNAME;//copy property one by one 
        db.SubmitChanges();
        return employee.PKEY;
    }
2) disable ObjectTrackingEnabled as following
// but in this case lazy loading is not supported
       
    
        public AppEmployeeDataContext() : 
            base(global::LinqLibrary.Properties.Settings.Default.AppConnect3DBConnectionString, mappingSource)
          {
                    this.ObjectTrackingEnabled = false;
           OnCreated();
          }
3) Detach all the related objects
partial class App3_EMPLOYEE
    {
        public void Detach()
        {
            this._APP3_EMPLOYEE_EXTs = default(EntityRef<app3_employee_ext>);
        }
    }

     public int updateEmployee(App3_EMPLOYEE employee)
    {
        AppEmployeeDataContext db = new AppEmployeeDataContext();
        employee.Detach();
        db.App3_EMPLOYEEs.Attach(employee,true);
        db.SubmitChanges();
        return employee.PKEY;
    }
</app3_employee_ext>

4) use Time stamp in the column http://www.west-wind.com/weblog/posts/135659.aspx

5) Create stored procedure for updating you data and call it by db context

Note : If you have any other way please do add it in comment I tried to include that in my article. 

Find Post on my Blog : http://pranayamr.blogspot.in/2012/03/most-efficient-way-to-update-with-linq.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)