0
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);

I am trying to create a new object with some same values as old object, But i am getting this error message;

Cannot add an entity that already exists.

Note: I don't want to delete OLD object. Kindly Help me I have google it but couldn't find any help or solution.

2 Answers 2

1

You need to create a new object, currently your new object is also reffering to old one:

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);
2
  • I know this solution but its so headaching! Feb 24, 2015 at 7:59
  • 1
    @WaqarAhmed you have to do this way, its reference type so that way won't work what you want Feb 24, 2015 at 9:13
0

If AgreementOld is DataTable then just do

var AgreementNew = AgreementOld.Copy();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.