title: MVC Entity Model Copy Object
body: <p>I have an object called <code>GeneralInformation</code> that I want to duplicate in my table, however, obviously this new record will have a different <code>GeneralInformationID</code>.</p>
  
  <p>My goal is to have the user click a link that goes to <code>domain.com/proforma/copyversion/&lt;id&gt;</code> where it does the action from my controller below.</p>
  
  <p><strong>Here's my Controller:</strong></p>
  
  <pre><code>[HttpPost]
  public ActionResult CopyVersion(int? id)
  {
      Version version = db.Versions.Find(id);
      GeneralInformation generalInformation = version.GeneralInformation;
  
      var generalInformationCopy = generalInformation;
      generalInformationCopy.GeneralInformationID = null;
  
      db.Entry(generalInformationCopy).State = EntityState.Added;
      return View("Index");
  }
  </code></pre>
  
  <p>I am receiving an error message of: <code>"Cannot convert source type 'null' to target type 'int'</code>.  The <code>GeneralInformationID</code> is the Primary Key and the Identity, auto generated column.</p>
  
  <p><strong>I have two tables in my Entity Model:</strong></p>
  
  <pre><code>GeneralInformation
  -------------------
  GeneralInformationID (PK)
  VersionID
  FirstName
  LastName
  </code></pre>
  
  <p>And my second table:</p>
  
  <pre><code>Version
  --------
  VersionID (PK)
  OwnerID
  VersionOwner
  VersionNumber
  isLocked
  </code></pre>
  
  <p>How do I make a COPY of the GeneralInformation object that I have?</p>
  
  <p><strong>EDIT - Updated Model:</strong> (Contains an error)</p>
  
  <pre><code>[HttpGet]
  public ActionResult CopyVersion(int? id)
  {
       Version version = Db.Versions.Find(id);
       version.isLocked = true;
       Db.Entry(version).State = EntityState.Modified;
  
       // Add new Version of the Proforma
       var newVersion = new Version() {
           VersionParentID = version.ProformaID,
           OwnerID = version.OwnerID,
           AuthorName = version.AuthorName,
           VersionNumber = (version.VersionNumber + 1)
       };
       Db.Entry(newVersion).State = EntityState.Added;
       Db.SaveChanges();
  
       // Create a copy of `GeneralInformation` and UPDATE the VersionID
       var generalInformation = proformaDb.GeneralInformations.Create();
       proformaDb.Entry&lt;GeneralInformation&gt;(version.GeneralInformation).CurrentValues.SetValues(generalInformation);
       Db.GeneralInformations.Add(generalInformation);
       Db.SaveChanges();
  
       // Redirect to the Proforma Index View
       return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>I'm getting the following error:</p>
  
  <p><code>The property 'VersionID' is part of the object's key information and cannot be modified.</code></p>
  
  <p>The VersionID is the PK on the table.</p>