Linq to SQL: How to Attach object to a different data context

After upgrading to Visual Studio 2008 RTM, you will have trouble updating Linq to SQL Classes which are read from one data context and then updated into another data context. You will get this exception during update:

System.NotSupportedException: 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.

Here's a typical example taken from a Forum post:

   1: public static void UpdateEmployee(Employee employee)
   2: {
   3: using (HRDataContext dataContext = new HRDataContext())
   4: {
   5: //Get original employee
   6: Employee originalEmployee = dataContext.Employees.Single(e=>e.EmployeeId==employee.EmployeeId);
   8: //attach to datacontext
   9: dataContext.Employees.Attach(employee, originalEmployee);
  11: //save changes
  12: dataContext.SubmitChanges();
  14: }
  15: }

When you call the Attach function, you will get the exception mentioned above.

Here's a way to do this. First, create a partial class that adds a Detach method to Employee class. This method will detach the object from it's data context and detach associated objects.

   1: public partial class Employee
   2: {
   3:   public void Detach()
   4:   {
   5:     this.PropertyChanged = null; this.PropertyChanging = null;
   7:     // Assuming there's a foreign key from Employee to Boss
   8:     this.Boss = default(EntityRef<Boss>);
   9:     // Similarly set child objects to default as well
  11:     this.Subordinates = default(EntitySet<Subordinate>);
  12:   }
  13: }

Now during update, call Detach before attaching the object to a DataContext.

   1: public static void UpdateEmployee(Employee employee)
   2: {
   3:     using (HRDataContext dataContext = new HRDataContext())
   4:     {
   5:         //attach to datacontext
   6:         employee.Detach();
   8:         dataContext.Employees.Attach(employee);
   9:         //save changes
  11:         dataContext.SubmitChanges();
  12:     }
  13: }

This'll work. It assumes the employee object already has its primary key populated.

You might see during update, it's generating a bloated UPDATE statement where each and every property is appearing on the WHERE clause. In that case, set UpdateCheck to Never for all properties of Employee class from the Object Relational Designer.


  • Do you have a more elegant way of doing this? Or perhaps a more lazy way, such as using reflection?

    Still it seems quite disappointing that we have to come to this.

  • I'm doing the same thing using a bit of reflection:

    public void DetachRelatedModels()
    Type ModelType = this.GetType();
    foreach (PropertyInfo Property in ModelType.GetProperties())
    if (Property.GetCustomAttributes(typeof(AssociationAttribute), false).GetLength(0) > 0)
    Type PropertyType = Property.PropertyType;
    Type[] ConstructorTypes = { };
    ConstructorInfo PropertyConstructor = PropertyType.GetConstructor(ConstructorTypes);
    object[] ConstructorValues = { };
    object DefaultProperty = PropertyConstructor.Invoke(ConstructorValues);
    Property.SetValue(this, DefaultProperty, null);


  • Why can i have a method that tells me if an object with a certain primary key is attached or not to the stupid datacontext ????

    Nooooooooo!!! Instead I have to detach everytime.


Comments have been disabled for this content.