C# MVC实体模型复制对象
我有一个名为GeneralInformation的对象,我想在我的表中复制它,但是,很明显,这个新记录将有一个不同的GeneralInformationID 我的目标是让用户单击指向domain.com/proforma/copyversion/的链接,然后从下面的控制器执行操作 这是我的控制器: 我收到错误消息:无法将源类型“null”转换为目标类型“int”。GeneralInformationID是主键和自动生成的标识列 我的实体模型中有两个表: 我的第二张桌子:C# MVC实体模型复制对象,c#,asp.net-mvc,entity-framework,asp.net-mvc-3,entity-framework-4,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 3,Entity Framework 4,我有一个名为GeneralInformation的对象,我想在我的表中复制它,但是,很明显,这个新记录将有一个不同的GeneralInformationID 我的目标是让用户单击指向domain.com/proforma/copyversion/的链接,然后从下面的控制器执行操作 这是我的控制器: 我收到错误消息:无法将源类型“null”转换为目标类型“int”。GeneralInformationID是主键和自动生成的标识列 我的实体模型中有两个表: 我的第二张桌子: Version ----
Version
--------
VersionID (PK)
OwnerID
VersionOwner
VersionNumber
isLocked
如何复制我拥有的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 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");
}
我得到以下错误:
属性“VersionID”是对象密钥信息的一部分,无法修改
VersionID是表上的主键。在操作中执行以下操作:
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();
或者阅读有关克隆实体的内容。如果GeneralInformationID是一个标识PK,那么我认为它是不可为空的。您是否尝试将其设置为值而不是null?例如:generalInformationCopy.GeneralInformationID=0;我收到一条错误消息:“GeneralInformation”类型的实体无法调用成员“CurrentValues”,因为该实体在上下文中不存在。要将实体添加到上下文,请调用DbSet的add或Attach方法。此外,这似乎只是更新GeneralInformation对象,而不是使用add和set值添加新的entry.swap行。您还可以创建新对象并手动将属性从主对象添加到该对象。我们是否应该添加generalInformationCopy?我想您可能需要。添加我们在第一行确认的generalInformation对象。mistype。当然,我们应该创建一个副本并添加一个副本
[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");
}
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();