簡體   English   中英

使用EntityFramework更新記錄時出錯

[英]Error When Updating A Record With EntityFramework

我想更新一條記錄,但程序會捕獲此錯誤

具有相同鍵的對象已存在於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;
}

為什么顯示錯誤,我該如何解決?

我相信伊戈爾是正確的。 若要解決此問題,請在用戶已經存在於數據庫中時更新用戶屬性,而不是項目屬性。 刪除代碼行“ 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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM