Multiple instances of IEntityChangeTracker cannot reference the same entity object.

ado.net-entity-data-model asp.net-mvc entity-framework linq-to-entities

Question

I now have the following code in an ASP.Net MVC project that uses the repository pattern:

    partial class Timesheet : ITimesheet
        {
            TimeSystemDBEntities _db;

            public Timesheet()
            {
                _db = new TimeSystemDBEntities();
            }
            .
            .
             //More Methods
            .
            .

            //Save WorkWeek to database
            private void SaveWorkWeek(WorkWeek wk)
                {
                    //Creating a new test context in attempt to resolve
                    //Error: “An entity object cannot be referenced by multiple instances of IEntityChangeTracker"
                    var testContext = new TimeSystemDBEntities();
                    var newWorkWeek = new WorkWeek();
                    var newStudent = new Student();
                    //Copy contents of wk to newWorkWeek
                    newWorkWeek.Students = newStudent.GetStudent(wk.Students.id);
                    newWorkWeek.MonDate = wk.MonDate;
                    newWorkWeek.MonHours = wk.MonHours;
                    newWorkWeek.TuesDate = wk.TuesDate;
                    newWorkWeek.TuesHours = wk.TuesHours;
                    newWorkWeek.WedDate = wk.WedDate;
                    newWorkWeek.WedHours = wk.WedHours;
                    newWorkWeek.ThursDate = wk.ThursDate;
                    newWorkWeek.ThursHours = wk.ThursHours;
                    newWorkWeek.FriDate = wk.FriDate;
                    newWorkWeek.FriHours = wk.FriHours;
                    newWorkWeek.TempSave = wk.TempSave;

                    testContext.AddToWorkWeek(newWorkWeek); //Exception thrown here
                    testContext.SaveChanges();

                    //Also tried
                    //_db.AddToWorkWeek(newWorkWeek); //Exception thrown here
                    //_db.SaveChanges();

                    //Also tried
                    //_db.detach(wk): //Different exception thrown here: Says it's not in ObjectStateManager
                    //_db.AddToWorkWeek(newWorkWeek); 
                    //_db.SaveChanges();

                    //Also tried
                    //_db.detach(wk.Students): //Different exception thrown here: Says it's not in ObjectStateManager
                    //_db.AddToWorkWeek(newWorkWeek); 
                    //_db.SaveChanges();

                }

    }

There is a 1-to-Many link between the classes Student and WorkWeek, respectively. I've tried everything to get the object to save, from detaching to building a brand-new context in the manner shown in the sample code above. When I attempt to disconnect, it essentially responds that it isn't in the ObjectStateManager, which I understand to mean that there isn't anything to detach, which, if true, would seem to contradict the first error. Any assistance would be very appreciated since I don't know what's happening.

1
2
4/14/2010 5:02:16 PM

Accepted Answer

Thanks, @Craig. I had previously looked at those sites and their solutions and tried to put them into practice, but I had no luck.

I found the answer to my issue in the following site, which also has additional extremely helpful details regarding various ad hoc Entity Framework-related problems.

Gotchas with Entity Framework

1
4/15/2010 3:44:13 PM

Popular Answer

MVC and the Entity Framework are new to me. After much struggle, I still had the same issue. This method solved it for me. I hope it will be beneficial to you folks.

var mediaItem = db.MediaItems.FirstOrDefault(x => x.Id == mediaItemViewModel.Id);
    mediaItem.Name = mediaItemViewModel.Name;
    mediaItem.Description = mediaItemViewModel.Description;
    mediaItem.ModifiedDate = DateTime.Now;
    mediaItem.FileName = mediaItem.FileName;
    mediaItem.Size = KBToMBConversion(mediaItemViewModel.Size);
    mediaItem.Type = mediaItem.Type;

//db.Entry(mediaItem).State = EntityState.Modified;// coment This line
db.SaveChanges();

Because you are reading the whole object from the database and holding it in the current context, it informs you there is already one entity associated to the context when you attempt to edit its state. Calling "save changes" will save the modifications.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow