Error: "Entity object cannot reference multiple instances of IEntityChangeTracker"

I am currently using the repository pattern in an ASP.Net MVC project and have the following code:

    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 not in ObjectStateManager
                    //_db.AddToWorkWeek(newWorkWeek); 
                    //_db.SaveChanges();

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

                }

    }

      

WorkWeek students and classes are in a 1-to-many relationship, respectively. I tried everything from detaching to creating a completely new context like in the above code example and can't get the object to save. When I try to disable it, it basically says that it is not in the ObjectStateManager, which I interpret as nothing to separate, which, if true, seems to contradict the original error. I'm not sure what's going on, any help is appreciated.

+2


a source to share


2 answers


@Craig, thanks for already looking at these links and their solutions and trying to implement them without success.

The following link provided me with a solution to my problem, also has other very useful information about other Entity Framework related issues.



Entity Framework Gotchas

+1


a source


I am new to MVC and Entity work. I have the same problem after a lot This solution worked for me. Hope this can be helpful for you guys.

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 db and keep it in the current context and when you try to change the state of the object it tells you already one entity bound to the current context. just call save changes, it will save it.

0


a source







All Articles