The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked

前端 未结 6 2212
日久生厌
日久生厌 2021-02-14 13:21

I am aware that such question has already been asked, but solution did not help me.

[Fact]
public async Task UpdateAsync()
{
    string newTitle = \"newTitle1\";         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-02-14 13:40

    In my case I hit this error when running SaveChanges twice inside of two IFs statements. I moved the SaveChanges outside of those two blocks of code. Just a side note in my service layer it is querying the data with AsNoTracking();

    if (user.SendPaymentStatus)
    {
        user.SendPaymentStatus = false;
        saveChanges = true;
        //_userService.SaveChanges(user, false);
    
        msg = GetPaymentHTML(user.MasterNodeName, user.Payee, DbMasterNode.LastPaidUtc);
        Framework.Email.SendEmail(email, "MasterNode Payment - " + user.MasterNodeName, msg);        
    }
    
    if (user.SendNodeStatus)
    {
        user.SendNodeStatus = false;
        saveChanges = true;
        //_userService.SaveChanges(user, false);
    
        msg = GetStatusHTML(user.MasterNodeName, user.Payee, DbMasterNode.CurrentStatus, DbMasterNode.LastSeenUtc);
        Framework.Email.SendEmail(email, "MasterNode Down - " + user.MasterNodeName, msg);
    }
    
    if (saveChanges)
    {
        user.SendPaymentStatus = false;
        _userService.SaveChanges(user, false);
    }
    

提交回复
热议问题