Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid off "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"?

I have a model in Ado.Net EF. I have a one to many relation and when I want to Add the entities I get the error

"An entity object cannot be referenceed by multiple instances of IEntityChangeTracker"

Any clue?

Something similar to

Template template = new Template();
...
...
while (from < to)
{
    Course course = new Course();
    .....
    template.Course.Add(course);
    .....
}
courseEntities.AddToTemplate(template); // Problem line
courseEntities.SaveChanges();
like image 259
Mariano Avatar asked Mar 06 '09 19:03

Mariano


1 Answers

I was getting this message until i started to store the data context in the HttpContext.Items Property. This means you can use the same data context for the current web request. That way you don't end up with 2 data contexts referencing the same entities.

Here is a good post on DataContext Life Management.

I hope it helps.

Dave

like image 148
CraftyFella Avatar answered Oct 18 '22 10:10

CraftyFella