【问题标题】:Error When Updating A Record With EntityFramework使用 EntityFramework 更新记录时出错
【发布时间】:2016-09-20 05:28:54
【问题描述】:

我想更新一条记录,但程序发现了这个错误

ObjectStateManager 中已存在具有相同键的对象。 ObjectStateManager 无法跟踪具有相同键的多个对象。”

这是我的代码

public bool Update(User item, HttpPostedFileBase avatar)
{
    var tran = ContextEntities.Database.BeginTransaction(IsolationLevel.ReadUncommitted);
    try
    {
        var user = new UserDa().Get(ContextEntities, item.Id);//get current user
        CheckConstraint(item, Enums.Status.Update);
        //avatar checker
        if (avatar != null)
        {
            if (avatar.ContentType != "image/jpeg")
                throw new Exception("[Only Jpg Is Allowed");

            if (user.AvatarId == null)
            {
                item.AvatarId = new FileDa().Insert(ContextEntities, avatar);
            }
            else if (user.AvatarId != null)
            {
                item.AvatarId = new FileDa().Update(ContextEntities, (Guid)user.AvatarId, avatar);
            }
        }
        //password checker
        item.Password = string.IsNullOrWhiteSpace(item.Password) ? user.Password : Utility.Hash.Md5(item.Password);
        ContextEntities.Entry(item).State = EntityState.Modified;
        if (!new UserDa().Update(ContextEntities, item))
            throw new Exception();
        tran.Commit();
        return true;
    }
    catch (Exception ex)
    {
        tran.Rollback();
        throw new Exception(ex.Message);
    }
}

这是我在 UserDa 类中的更新方法

public bool Update(PortalEntities contextEntities, User item)
{
    var res =  contextEntities.SaveChanges() > 0;
    return res;
}

为什么会显示错误以及如何修复它?

【问题讨论】:

  • 可能是因为实例 item 已被跟踪,现在您正在检索或添加重复的实例到行 var user = new UserDa().Get(ContextEntities, item.Id);//get current user。只是猜测,因为您没有提供此方法的代码。

标签: c# asp.net-mvc entity-framework


【解决方案1】:

我相信伊戈尔是正确的。要解决此问题,当用户已存在于数据库中时,请更新用户属性而不是项目属性。删除代码行“ContextEntities.Entry(item).State = EntityState.Modified;”并保存更改。

public bool Update(User item, HttpPostedFileBase avatar)
{
    var tran = ContextEntities.Database.BeginTransaction(IsolationLevel.ReadUncommitted);
    try
    {
        var user = new UserDa().Get(ContextEntities, item.Id);//get current user
        CheckConstraint(item, Enums.Status.Update);

        if(user == null)
        {
            //throw and error or do something else
        }}

        //avatar checker
        if (avatar != null)
        {
            if (avatar.ContentType != "image/jpeg")
                throw new Exception("[Only Jpg Is Allowed");

            if (user.AvatarId == null)
            {
                user.AvatarId = new FileDa().Insert(ContextEntities, avatar);
            }
            else if (user.AvatarId != null)
            {
                user.AvatarId = new FileDa().Update(ContextEntities, (Guid)user.AvatarId, avatar);
            }
        }
        //password checker
        user.Password = string.IsNullOrWhiteSpace(item.Password) ? user.Password : Utility.Hash.Md5(user.Password);
        //ContextEntities.Entry(item).State = EntityState.Modified;
        if (!new UserDa().Update(ContextEntities, user))
            throw new Exception();
        tran.Commit();
        return true;
    }
    catch (Exception ex)
    {
        tran.Rollback();
        throw new Exception(ex.Message);
    }
}

【讨论】:

  • 另外,请务必更新可能已在项目上更新的任何其他用户属性。 AutoMapper 非常适合这类事情。
猜你喜欢
  • 2017-06-09
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多