0

I have an object called GeneralInformation that I want to duplicate in my table, however, obviously this new record will have a different GeneralInformationID.

My goal is to have the user click a link that goes to domain.com/proforma/copyversion/<id> where it does the action from my controller below.

Here's my Controller:

[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");
}

I am receiving an error message of: "Cannot convert source type 'null' to target type 'int'. The GeneralInformationID is the Primary Key and the Identity, auto generated column.

I have two tables in my Entity Model:

GeneralInformation
-------------------
GeneralInformationID (PK)
VersionID
FirstName
LastName

And my second table:

Version
--------
VersionID (PK)
OwnerID
VersionOwner
VersionNumber
isLocked

How do I make a COPY of the GeneralInformation object that I have?

EDIT - Updated Model: (Contains an error)

[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<GeneralInformation>(version.GeneralInformation).CurrentValues.SetValues(generalInformation);
     Db.GeneralInformations.Add(generalInformation);
     Db.SaveChanges();

     // Redirect to the Proforma Index View
     return RedirectToAction("Index");
}

I'm getting the following error:

The property 'VersionID' is part of the object's key information and cannot be modified.

The VersionID is the PK on the table.

Turp
  • 708
  • 3
  • 14
  • 26
  • 1
    If `GeneralInformationID` is an `IDENTITY` PK, then I imagine it's non-nullable. Have you tried setting it to a value instead of `null`? For example: `generalInformationCopy.GeneralInformationID = 0;` – David Oct 15 '14 at 18:26

1 Answers1

0

Do this in your action:

GeneralInformation generalInformationCopy = db.GeneralInformations.Create(); // i assume that you have this dbset in your contex
db.GeneralInformations.Add(generalInformationCopy);     
db.Entry<GeneralInformation>(generalInformationCopy).CurrentValues.SetValues(generalInformation);
db.SaveChanges();

Or read this, about cloning entities.

Community
  • 1
  • 1
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • I got an error message: `Member 'CurrentValues' cannot be called for the entity of type 'GeneralInformation' because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet.` – Turp Oct 15 '14 at 19:19
  • Also, it seems that this is just UPDATING the GeneralInfomation object and not Adding a new entry. – Turp Oct 15 '14 at 19:58
  • swap rows with add and set values. Also you can just create new object and add properties manually from main object to it. – aleha_84 Oct 15 '14 at 20:26
  • Should we be *adding* the `generalInformationCopy`? I would think that you would need to .Add the `generalInformation` object we creted on the first line. – Turp Oct 16 '14 at 14:02
  • mistype. ofcourse we should create a copy and add a copy – aleha_84 Oct 16 '14 at 19:46