Turp
Turp

Reputation: 708

Property 'VersionID' is part of the object's key information and cannot be modified

I am receving an error message when I attempt to make a COPY of an object, reassign the PK ID and then Add the object to the GeneralInformation Model.

I have two tables in my Entity Model:

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

And my second table:

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

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

Here's My Controller:

[HttpGet]
public ActionResult CopyVersion(int? id)
{
     Version version = Db.Versions.Find(id);
     version.isLocked = true;
     Db.Entry(version).State = EntityState.Modified;

     // Add new Version
     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
     GeneralInformation generalInformation = new GeneralInformation();

     // Make both VersionID's the same.
     generalInformation.VersionID = newVersion.VersionID;
     version.GeneralInformation.VersionID = newVersion.VersionID;

     var currentValues = Db.Entry<GeneralInformation>(version.GeneralInformation).CurrentValues;
     currentValues.SetValues(generalInformation); //**ERRORS OUT ON THIS LINE**
     generalInformation.VersionID = newVersion.ProformaID;
     Db.GeneralInformations.Add(generalInformation);

     // 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.

Note: The VersionID of GeneralInformation is the PK on the table i'm attempting to copy.

Note: There is a relationship between Version and GenralInformation of 1 to 0..1

Upvotes: 0

Views: 840

Answers (1)

wahwahwah
wahwahwah

Reputation: 3177

The "VersionId" property of your General Information entity should be a foreign key not a primary key.

Upvotes: 1

Related Questions