0
當我嘗試複製對象時,重新分配PK ID,然後將該對象添加到GeneralInformation模型中,我接收到錯誤消息。屬性'VersionID'是對象的關鍵信息的一部分,無法修改
我有我的實體模型的兩個表:
Version
--------
VersionID (PK)
OwnerID
VersionOwner
VersionNumber
我的第二個表:
GeneralInformation
-------------------
GeneralInformationID (Identity)
VersionID (PK)
FirstName
LastName
如何讓GeneralInformation對象的副本,我有?
這裏是我的控制器:
[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");
}
,我發現了以下錯誤:
The property 'VersionID' is part of the object's key information and cannot be modified.
注:的GeneralInformation
的VersionID
在桌子上我」的PK m試圖複製。
注:還有就是1 to 0..1