C# 这可能吗,DbContext.SaveChanges() 返回 0 但没有异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11097518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:27:56  来源:igfitidea点击:

Is that possible, DbContext.SaveChanges() returns 0 but doesn't have an exception?

c#asp.netentity-frameworkc#-4.0entity-framework-4

提问by cagin

I use Entity Framework 4.0. Is it possible that SaveChanges()returns 0 but doesn't throw an exception? For example, after adding.

我使用实体框架 4.0。是否有可能SaveChanges()返回 0 但不抛出异常?例如添加后。

Here is my code:

这是我的代码:

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

采纳答案by Klaus Byskov Pedersen

According to the documentation, the return value of DbContext.SaveChangesis

根据文档,返回值DbContext.SaveChanges

The number of objects written to the underlying database.

写入底层数据库的对象数。

So what you see is only possible, when no entities needed to be saved to the database.

所以你看到的只有在不需要将实体保存到数据库时才有可能。

回答by David

Entity Framework's db.SaveChanges()for deletes and saves returns the number rows being affected. In test using the Fakes Framework (stubs and shims), however, the value returned will always be 0.

实体框架的db.SaveChanges()删除和保存返回受影响的行数。然而,在使用 Fakes 框架(存根和垫片)进行测试时,返回的值将始终为 0。

If there is an error in the call, then an exception will be thrown. The implication is that any calling method relying on a value greater than zero returned from db.SaveChanges()for confirmation cannot be tested for that same value.

如果调用中有错误,则会抛出异常。这意味着任何依赖于从db.SaveChanges()确认返回的大于零的值的调用方法都不能针对相同的值进行测试。

This may prove critical when a method is using the db.SaveChanges()return value to evaluate the number of rows affected in a given operation.

当方法使用db.SaveChanges()返回值来评估给定操作中受影响的行数时,这可能很重要。

回答by Tan

My answer is not mentioning about DbContext, but if someone is using XEntitiesand getting same problem, you can try:

我的回答没有提到 about DbContext,但如果有人正在使用XEntities并遇到同样的问题,您可以尝试:

using (var entities = new XEntities())
{
    var product = entities.Products.SingleOrDefault(x => x.Id == id);
    product.Type = "New type"; // modifying

    int flag = entities.SaveChanges(); // 1
    // or await entities.SaveChangesAsync(); // 1
}

The problem:

问题:

public class Demo
{
    private XEntities _entities = new XEntities();

    public void Test(int id)
    {
        var product = _entities.Product.SingleOrDefault(x => x.Id == id);
        product.Type = "New type"; // modifying

        int flag = _entities.SaveChanges(); // 0 <<<====
        // or await entities.SaveChangesAsync(); // 0  <<<====

        // you need to create new instance of XEntities to apply changes
    }
}