tblAgreement AgreementOld = (from g in TDC.tblAgreements
where g.AccountID == ids.accountID && g.AgreementID == AgreementID
select g).SingleOrDefault();
Guid AgreementIDNew = Guid.NewGuid();
var AgreementNew = AgreementOld;
AgreementNew.AgreementID = AgreementIDNew;
AgreementNew.StatusChangeDate = DateTime.Now;
AgreementNew.CreationDate = DateTime.Now;
AgreementNew.AccountID = ids.accountID;
AgreementNew.CreatedByID = ids.userID;
TDC.tblAgreements.InsertOnSubmit(AgreementNew);
我正在尝试创建一个与旧对象具有相同值的新对象,但我收到此错误消息;
无法添加已存在的实体。
注意:我不想删除OLD对象。请帮助我,我有谷歌,但无法找到任何帮助或解决方案。
答案 0 :(得分:1)
您需要创建一个新对象,目前您的新对象也指旧对象:
var AgreementNew = new Agreement(); // create a new instance
AgreementNew.SomeProperty = AgreementOld.SomeProperty; // assign property from old to new
AgreementNew.AgreementID = AgreementIDNew; // new proerties here
AgreementNew.StatusChangeDate = DateTime.Now;
AgreementNew.CreationDate = DateTime.Now;
AgreementNew.AccountID = ids.accountID;
AgreementNew.CreatedByID = ids.userID;
TDC.tblAgreements.InsertOnSubmit(AgreementNew);
答案 1 :(得分:0)
如果AgreementOld是DataTable,那么只需执行
var AgreementNew = AgreementOld.Copy();