C# 财产';版本ID';是对象的一部分';s键信息,无法修改
当我尝试复制一个对象,重新分配PK ID,然后将该对象添加到GeneralInformation模型时,我收到一条错误消息 我的实体模型中有两个表:C# 财产';版本ID';是对象的一部分';s键信息,无法修改,c#,asp.net-mvc,entity-framework,model-view-controller,C#,Asp.net Mvc,Entity Framework,Model View Controller,当我尝试复制一个对象,重新分配PK ID,然后将该对象添加到GeneralInformation模型时,我收到一条错误消息 我的实体模型中有两个表: Version -------- VersionID (PK) OwnerID VersionOwner VersionNumber [HttpGet] public ActionResult CopyVersion(int? id) { Version version = Db.Versions.Find(id); vers
Version
--------
VersionID (PK)
OwnerID
VersionOwner
VersionNumber
[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");
}
和我的第二张桌子:
Version
--------
VersionID (PK)
OwnerID
VersionOwner
VersionNumber
[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");
}
如何复制我拥有的GeneralInformation对象
这是我的控制器:
Version
--------
VersionID (PK)
OwnerID
VersionOwner
VersionNumber
[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");
}
[HttpGet]
公共操作结果副本版本(int?id)
{
Version Version=Db.Versions.Find(id);
version.isLocked=true;
Db.Entry(version.State=EntityState.Modified;
//添加新版本
var newVersion=新版本(){
VersionParentID=version.proformId,
OwnerID=version.OwnerID,
AuthorName=version.AuthorName,
VersionNumber=(version.VersionNumber+1)
};
Db.Entry(newVersion).State=EntityState.Added;
Db.SaveChanges();
//创建“GeneralInformation”的副本并更新版本ID
广义信息广义信息=新的广义信息();
//使两个版本ID相同。
generalInformation.VersionID=newVersion.VersionID;
version.GeneralInformation.VersionID=newVersion.VersionID;
var currentValues=Db.Entry(version.GeneralInformation).currentValues;
currentValues.SetValues(通用信息);//**此行出错**
generalInformation.VersionID=newVersion.ProformId;
Db.generalInformation.Add(generalInformation);
//重定向到形式索引视图
返回操作(“索引”);
}
我得到以下错误:
属性“VersionID”是对象密钥信息的一部分,无法修改。
注意:通用信息的VersionID
是我试图复制的表上的主键
注意:版本与1到0..1的GenralInformation之间存在关系。
常规信息实体的“VersionId”属性应为外键,而不是主键