0

I want to copy full information of record to another in EF C#:

List<Cohort> AnYearsCohorts = oDB.Cohorts.Where(x=>x.Year == oCh.Year).ToList();
foreach (Cohort ochort in AnYearsCohorts)
{
    Cohort nChort = new Cohort();
    nChort = ochort;
    nChort.Year = Year;
    nChort.ID = 0;
    oDB.Cohorts.Add(nChort);
}
oDB.SaveChanges();

But got following error:

{"The property 'ID' is part of the object's key information and cannot be modified. "}

1
  • just remove this line: nChort.ID = 0; Commented Jun 15, 2017 at 12:42

1 Answer 1

3

You probably have set ID as primary key and auto incremented so you can not change ID field explicitly in code. Either remove ID as key or let ID increment automatically.

Sign up to request clarification or add additional context in comments.

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.