加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 这是可能的,DbContext.SaveChanges()返回0但没有异常?

发布时间:2020-12-15 06:18:22 所属栏目:百科 来源:网络整理
导读:我使用Entity Framework 4.0. SaveChanges()可能返回0但不会引发异常?例如,添加后. 这是我的代码: try{ _context.CodeProducts.Add(entity); _context.SaveChanges(); //Shell I control return result from SaveChanges() in here. //However doesn't thr
我使用Entity Framework 4.0. SaveChanges()可能返回0但不会引发异常?例如,添加后.

这是我的代码:

try
{
    _context.CodeProducts.Add(entity);
    _context.SaveChanges();

    //Shell I control return result from SaveChanges() in here.
    //However doesn't throw an exceoption?

    return new MethodResponse()
    {
        ResultText = "Successful",Type = MethodResponse.ResponseType.Succeed
    };
}
catch (OptimisticConcurrencyException exc)
{
    throw exc;
}
catch (UpdateException exc)
{
    throw exc;
}
catch (Exception exc)
{
    throw exc;
}

解决方法

根据 the documentation,DbContext.SaveChanges的返回值为

The number of objects written to the underlying database.

所以你看到的只有在没有实体需要保存到数据库的时候才可能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读