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);
Sign up to request clarification or add additional context in comments.

2 Comments

I know this solution but its so headaching!
@WaqarAhmed you have to do this way, its reference type so that way won't work what you want
0

If AgreementOld is DataTable then just do

var AgreementNew = AgreementOld.Copy();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.