Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可能,DbContext.SaveChanges()返回0,但不返回';你没有例外吗?_C#_Asp.net_Entity Framework_C# 4.0_Entity Framework 4 - Fatal编程技术网

C# 是否可能,DbContext.SaveChanges()返回0,但不返回';你没有例外吗?

C# 是否可能,DbContext.SaveChanges()返回0,但不返回';你没有例外吗?,c#,asp.net,entity-framework,c#-4.0,entity-framework-4,C#,Asp.net,Entity Framework,C# 4.0,Entity Framework 4,我使用实体框架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

我使用实体框架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;
}
根据,DbContext.SaveChanges的返回值为

写入基础数据库的对象数


因此,只有当不需要将任何实体保存到数据库时,您才能看到。Entity Framework的“删除和保存”的
db.SaveChanges()
返回受影响的行数。然而,在使用Fakes框架(存根和垫片)的测试中,返回的值始终为0

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


当一个方法使用
db.SaveChanges()
返回值来评估给定操作中受影响的行数时,这可能证明是至关重要的

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

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
}
问题是:

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
    }
}
公共类演示
{
私有XEntities _entities=新XEntities();
公共无效测试(int id)
{
var product=\u entities.product.SingleOrDefault(x=>x.Id==Id);
product.Type=“新类型”;//修改

int flag=_entities.SaveChanges();//0注意到,如果您更新了一个实体,但它实际上没有任何更改
savechangessync()
将返回
0
异常
将为
null

您需要添加更多信息,否则没有人能够提供帮助。您可以发布代码吗?没错-例如:如果删除不再存在的行,您将受到0的影响rows@CarstenKönig No,如果删除不存在的行st之后,数据库将返回“受影响的0行”,EF将其转换为异常。“无需保存任何实体”意味着在调用
SaveChanges
之前上下文中没有任何更改。真的吗?我必须尝试一下,但我认为我过去也遇到过类似的问题,没有出现异常,但感谢您的更正,如果它返回-1?