2

故事:

当我尝试保存某些内容时出现一个奇怪的错误,我收到了此错误消息

一个实体对象不能被多个 IEntityChangeTracker 实例引用。

我真的不知道那是什么,为什么会出现,只有当我尝试保存我的插入和更新正在工作的东西时才会出现,只有当我尝试从 Telerik 网格中将某些东西保存在 db 中时才会出现

    if (this.annualVacationList != null)
        {
            List<AnnualVacation> vacationToSave = this.annualVacationList;
            IEnumerable<AnnualVacation> existing = paramUser.AnnualVacations;
            foreach (AnnualVacation toSave in vacationToSave)
            {
                AnnualVacation existingItem = existing.Where(x => x.AnnualVacationId == toSave.AnnualVacationId).SingleOrDefault();
                if (existingItem == null)
                {
                    ctx.AddToAnnualVacations(toSave);
                }
                else
                {
                    existingItem.FromDate = toSave.FromDate;
                    existingItem.ToDate = toSave.ToDate;
                    existingItem.WorkingTime = toSave.WorkingTime;
                    existingItem.VacationDays = toSave.VacationDays;
                }
            }
        }
        ctx.SaveChanges();
    }

调试后,我看到代码在 Project.Name.Designer.cs 中停止运行 ..... Oo

  public void AddToAnnualVacations(AnnualVacation annualVacation)
    {
        base.AddObject("AnnualVacations", annualVacation);
    }
4

1 回答 1

0

嘿嘿

我自己得到了它,视图状态并没有分离上下文,这就是我刚刚创建的问题

var tmp = new AnnualVacation
                    {
                        FromDate = toSave.FromDate,
                        ToDate = toSave.ToDate,
                        WorkingTime = toSave.WorkingTime,
                        VacationDays = toSave.VacationDays,
                        UserId = toSave.UserId
                    };

在上面的相同 if 查询中 (if(existing Item==null)) 并且它有效,但仍然感谢所有试图帮助我的人^^

于 2012-12-04T14:19:17.437 回答