intermittent System.Data.Entity.Infrastructure.DbUpdateConcurrencyException

天大地大妈咪最大 提交于 2019-12-25 04:11:48

问题


The following code is causing an intermittent exception:

public int UnblockJob(int jobId)
{
    using (var connect = MakeConnect())
    {
        var tag = connect.JobTag.SingleOrDefault(jt => jt.JobId == jobId && jt.Name == Metrics.TagNameItemBlockCaller);
        if (tag == null)
        {
            return 0;
        }
        connect.JobTag.Remove(tag);
        return connect.SaveChanges();
    }
}

How can I correct or troubleshoot it?


回答1:


From the documentation for DbUpdateConcurrencyException:

Exception thrown by DbContext when it was expected that SaveChanges for an entity would result in a database update but in fact no rows in the database were affected.

This means that the record you are attempting to delete has since been removed from the database. It would appear that you have another process that is deleting records or this function is able to be called concurrently.

There are several solutions, here are a couple:

Fix the source problem Stop other processes affecting the data.

Catch the error Wrap this method in a try/catch block, after all you may only care that the record has been deleted:

try
{
    //Existing code here
}
catch(DbUpdateConcurrencyException)
{
    //Safely ignore this exception
}
catch(Exception e)
{
    //Something else has occurred
    throw;
}


来源:https://stackoverflow.com/questions/28660708/intermittent-system-data-entity-infrastructure-dbupdateconcurrencyexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!