0

I have the following code:

Chart getChart = _db.Charts.SingleOrDefault(p => p.ChartID == chart.ChartID);
if (ModelState.IsValid)
        {
            getChart.MainChart = false;
            _db.Charts.AddObject(getChart);
            _db.SaveChanges();

So I want to duplicate the row. But I'm getting this error: An object with the same key already exists in the ObjectStateManager. The existing object is in the Modified state. An object can only be added to the ObjectStateManager again if it is in the added state.

What is the easiest way to change the Identity column to let met add this row. I don't want to create a new Chart because there is allot of columns in that table.

Kind Regards

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291

1 Answers1

0

AFAIK What you're trying to achieve is impossible with your approach.
You'd have to use a mapping library like AutoMapper, or implement IClonable for your Chart entity and use Clone() method.

Update:
Also, you can write/use a generic method to copy each property to the similar one in anothre object. A couple of useful links are this article on CodeProject or this blog post.

Kamyar
  • 18,639
  • 9
  • 97
  • 171