2014-10-16 19 views
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.

注:GeneralInformationVersionID在桌子上我”的PK m试图复制。

注:还有就是1 to 0..1

回答

1

您的基本信息实体的“VERSIONID”属性应该是一个外键不是主键的版本和GenralInformation之间的关系。

相关问题