title: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext
body: <p>I've got a problem with NotSupportedException, I'm getting:
  "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext."</p>
  
  <pre><code>partial class SupplyOfert : Model
  {
      public SupplyOfert(int id = 0)
      {
          if (id &gt; 0)
              this.get(id);
      }
  
      public Supplier supplier
      {
          get
          {
              return this.Supplier;
          }
          set
          {
              this.Supplier = db.Suppliers.SingleOrDefault(s =&gt; s.id == value.id);
          }
      }
  
      public override bool get(int id)
      {
          SupplyOfert so = (SupplyOfert)db.SupplyOferts.SingleOrDefault(s =&gt; s.id == id).Copy(this, "Supplies");
          this._Supplies = so._Supplies;
          so._Supplies.Clear();
          if (this.id &gt; 0)
          {
              db.Dispose();
              db = new Database();
              db.SupplyOferts.Attach(this);  // here I'm getting the exception
              return true;
          }
          else
              return false;
      }
  
      public override void save()
      {
          if (this.id &lt;= 0)
              db.SupplyOferts.InsertOnSubmit(this);
          db.SubmitChanges();
      }
  
      public override void remove()
      {
          if (this.id &gt; 0)
          {
              db.SupplyOferts.DeleteOnSubmit(this);
              db.SubmitChanges();
          }
      }
  }
  </code></pre>
  
  <p>When I add new record with:</p>
  
  <pre><code>SupplyOfert ofert = new SupplyOfert();
  ofert.price = 100;
  ofert.publisher = "some publisher";
  ofert.supplier = new Supplier(1);
  ofert.title = "some title";
  ofert.year = DateTime.Today;
  ofert.save();
  </code></pre>
  
  <p>It's ok, but when I want to update record, using:</p>
  
  <pre><code>SupplyOfert ofert = new SupplyOfert(2);
  ofert.price = 220;
  ofert.publisher = "new publisher";
  ofert.save();
  </code></pre>
  
  <p>I'm getting "NotSupportedExceptionw was unhandled".</p>
  
  <p>Here's the Model class:</p>
  
  <pre><code>public abstract class Model
  {
      protected Database db = new Database();
  
      public Model(){}
  
      ~Model()
      {
          db.Dispose();
      }
  
      abstract public bool get(int id);
      abstract public void save();
      abstract public void remove();
  
  }
  </code></pre>
  
  <p>And here's the copy method:</p>
  
  <pre><code>public static class ObjectProperties
  {
      public static object Copy(this object source, object destination, string skip = "")
      {
          if (source != null)
          {
              foreach (var sourceProperty in source.GetType().GetProperties())
              {
                  foreach (var destinationProperty in destination.GetType().GetProperties())
                  {
                      if (destinationProperty.Name == sourceProperty.Name &amp;&amp; destinationProperty.Name != skip)
                          destinationProperty.GetSetMethod().Invoke(destination, new object[] { sourceProperty.GetGetMethod().Invoke(source, null) });
                  }
              }
          }
          else
              destination = null;
          return source;
      }
  }
  </code></pre>
  
  <p>Database class it's only:</p>
  
  <pre><code>public class Database : DataClasses1DataContext
  </code></pre>
  
  <hr>
  
  <p>I've found out, that this problem is connected with assignment:</p>
  
  <pre><code>this.supplier = so.supplier;
  </code></pre>
  
  <p>But I don't know why...</p>