间歇性System.Data.Entity.Infrastructure.DbUpdateConcurrencyException [英] intermittent System.Data.Entity.Infrastructure.DbUpdateConcurrencyException

查看:83
本文介绍了间歇性System.Data.Entity.Infrastructure.DbUpdateConcurrencyException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码导致间歇性异常:

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();
    }
}

如何纠正或解决问题?

推荐答案

来自 DbUpdateConcurrencyException


DbContext 引发的异常,当期望 SaveChanges 因为一个实体将导致数据库更新,但实际上数据库中没有行受到影响。

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.

错误将此方法包装在 try / catch 块中,毕竟您可能只关心记录已被删除:

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;
}

这篇关于间歇性System.Data.Entity.Infrastructure.DbUpdateConcurrencyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆