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

发布于 2024-08-28 17:06:26 字数 2552 浏览 6 评论 0原文

我当前正在 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 类分别处于一对多关系。我已经尝试了从分离到创建全新上下文的所有方法,如上面的示例代码所示,但无法保存对象。当我尝试分离时,它基本上说它不在 ObjectStateManager 中,我将其解释为没有任何东西可以分离,如果是真的,这似乎与原始错误相矛盾。我不确定发生了什么事,非常感谢任何帮助。

I'm 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'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();

                }

    }

The Student and WorkWeek classes are in a 1-to-Many relationship, respectively. I have tried everything from detaching to creating a completely new context as in the example code above and cannot get the object to save. When I try and detach it basically says it's not in the ObjectStateManager, which I interpret as there's nothing to detach, which ,if true, seems to contradict the original error. I'm not sure what's going on, any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷清清 2024-09-04 17:06:26

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

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

实体框架问题

@Craig, thanks I had actually already looked at those links and their solutions and attempted to implement them with no success.

The following link provided me with the solution to my problem, also has other very useful information about other miscellaneous issues that have to do with the Entity Framework.

Entity Framework Gotchas

生生漫 2024-09-04 17:06:26

我是 MVC 和 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();

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

I am new to MVC & Entity frame work. I got same problem after fighting a lot
This solution worked for me. Hope it can be useful 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();

Cause you are reading the the whole object from db and holding it in the current context and when you try to modify the entity state its tells you already one entity attached to the current context. just call save changes it will save it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文