错误:“IEntityChangeTracker的多个实例无法引用实体对象”

时间:2010-04-14 17:02:17

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

我目前正在ASP.Net MVC项目中使用存储库模式,并且具有以下代码:

    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();

                }

    }

Student和WorkWeek课程分别处于1对多的关系中。我已经尝试了从分离到创建全新上下文的所有内容,如上面的示例代码中所示,并且无法保存对象。当我尝试分离时,它基本上说它不在ObjectStateManager中,我解释为没有什么可以分离,如果是真的,它似乎与原始错误相矛盾。我不确定发生了什么,感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

@Craig,谢谢我实际上已经查看了这些链接及其解决方案并试图实现它们但没有成功。

以下链接为我提供了问题的解决方案,还提供了有关与实体框架有关的其他杂项问题的其他非常有用的信息。

Entity Framework Gotchas

答案 1 :(得分:0)

我是MVC&的新手实体框架工作。经过多次战斗后,我遇到了同样的问题 这个解决方案对我有用。希望它对你们有用。

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();

因为您正在从db读取整个对象并将其保存在当前上下文中,并且当您尝试修改实体状态时,它会告诉您已经有一个实体附加到当前上下文。只需调用保存更改就可以保存它。