Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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# 基于数据库值的实体框架更新?_C#_Sql_Entity Framework_Concurrency - Fatal编程技术网

C# 基于数据库值的实体框架更新?

C# 基于数据库值的实体框架更新?,c#,sql,entity-framework,concurrency,C#,Sql,Entity Framework,Concurrency,在Entity Framework之前,如果我有某个产品的库存数量或订单数量,我会使用当前数据库值更新其数量,如下所示: UPDATE Products SET AvailableQty = AvailableQty - 2 WHERE ProductId = 1; 有没有一种方法可以用实体框架完成同样的事情?框架似乎遵循加载、修改和更新模式: Product product = db.Products.FirstOrDefault(x => x.ProductId == 1); pro

在Entity Framework之前,如果我有某个产品的库存数量或订单数量,我会使用当前数据库值更新其数量,如下所示:

UPDATE Products SET AvailableQty = AvailableQty - 2 WHERE ProductId = 1;
有没有一种方法可以用实体框架完成同样的事情?框架似乎遵循加载、修改和更新模式:

Product product = db.Products.FirstOrDefault(x => x.ProductId == 1);
product.AvailableQty += 2;
db.SaveChanges();
然而,按照这种方法,产品可能在数据的初始加载和更新之间发生变化。我知道我可以在实体上设置一个并发字段来阻止更新,但在大多数情况下,我不希望用户干预(例如客户下订单或接收采购订单)

有没有更好的方法可以使用EF来处理类似的情况,或者对于这些情况,我应该回到原始SQL吗?

首选方法是基于意见的,所以我将集中讨论答案。EF允许您通过DbContext的数据库属性直接访问数据库。您可以使用ExecuteSqlCommand直接执行SQL。或者,您可以使用SqlQuery扩展方法

ExecuteSqlCommand返回受影响的记录。并且,SqlQuery扩展方法允许您使用EF提供的填充

此外,如果功率不足,您可以创建自己的命令,如下所示:

var direct = mydbContext.Database;
using (var command = direct.Connection.CreateCommand())
{
    if (command.Connection.State != ConnectionState.Open) 
    { 
        command.Connection.Open(); 
    }

    command.CommandText = query.ToString(); // Some query built with StringBuilder.
    command.Parameters.Add(new SqlParameter("@id", someId));
    using (var reader = command.ExecuteReader())
    {
        if (reader.Read()) 
        {
            ... code here ...
            reader.Close();
        }
        command.Connection.Close();
    }
}
“首选方法”是基于意见的,所以我将集中精力回答这个问题。EF允许您通过DbContext的数据库属性直接访问数据库。您可以使用ExecuteSqlCommand直接执行SQL。或者,您可以使用SqlQuery扩展方法

ExecuteSqlCommand返回受影响的记录。并且,SqlQuery扩展方法允许您使用EF提供的填充

此外,如果功率不足,您可以创建自己的命令,如下所示:

var direct = mydbContext.Database;
using (var command = direct.Connection.CreateCommand())
{
    if (command.Connection.State != ConnectionState.Open) 
    { 
        command.Connection.Open(); 
    }

    command.CommandText = query.ToString(); // Some query built with StringBuilder.
    command.Parameters.Add(new SqlParameter("@id", someId));
    using (var reader = command.ExecuteReader())
    {
        if (reader.Read()) 
        {
            ... code here ...
            reader.Close();
        }
        command.Connection.Close();
    }
}
“首选方法”是基于意见的,所以我将集中精力回答这个问题。EF允许您通过DbContext的数据库属性直接访问数据库。您可以使用ExecuteSqlCommand直接执行SQL。或者,您可以使用SqlQuery扩展方法

ExecuteSqlCommand返回受影响的记录。并且,SqlQuery扩展方法允许您使用EF提供的填充

此外,如果功率不足,您可以创建自己的命令,如下所示:

var direct = mydbContext.Database;
using (var command = direct.Connection.CreateCommand())
{
    if (command.Connection.State != ConnectionState.Open) 
    { 
        command.Connection.Open(); 
    }

    command.CommandText = query.ToString(); // Some query built with StringBuilder.
    command.Parameters.Add(new SqlParameter("@id", someId));
    using (var reader = command.ExecuteReader())
    {
        if (reader.Read()) 
        {
            ... code here ...
            reader.Close();
        }
        command.Connection.Close();
    }
}
“首选方法”是基于意见的,所以我将集中精力回答这个问题。EF允许您通过DbContext的数据库属性直接访问数据库。您可以使用ExecuteSqlCommand直接执行SQL。或者,您可以使用SqlQuery扩展方法

ExecuteSqlCommand返回受影响的记录。并且,SqlQuery扩展方法允许您使用EF提供的填充

此外,如果功率不足,您可以创建自己的命令,如下所示:

var direct = mydbContext.Database;
using (var command = direct.Connection.CreateCommand())
{
    if (command.Connection.State != ConnectionState.Open) 
    { 
        command.Connection.Open(); 
    }

    command.CommandText = query.ToString(); // Some query built with StringBuilder.
    command.Parameters.Add(new SqlParameter("@id", someId));
    using (var reader = command.ExecuteReader())
    {
        if (reader.Read()) 
        {
            ... code here ...
            reader.Close();
        }
        command.Connection.Close();
    }
}

将您的查找和更新包含在事务中

using (var transaction = new System.Transactions.TransactionScope())
{
    Product product = db.Products.FirstOrDefault(x => x.ProductId == 1);
    product.AvailableQty += 2;
    db.SaveChanges();
    transaction.Complete();
}

将您的查找和更新包含在事务中

using (var transaction = new System.Transactions.TransactionScope())
{
    Product product = db.Products.FirstOrDefault(x => x.ProductId == 1);
    product.AvailableQty += 2;
    db.SaveChanges();
    transaction.Complete();
}

将您的查找和更新包含在事务中

using (var transaction = new System.Transactions.TransactionScope())
{
    Product product = db.Products.FirstOrDefault(x => x.ProductId == 1);
    product.AvailableQty += 2;
    db.SaveChanges();
    transaction.Complete();
}

将您的查找和更新包含在事务中

using (var transaction = new System.Transactions.TransactionScope())
{
    Product product = db.Products.FirstOrDefault(x => x.ProductId == 1);
    product.AvailableQty += 2;
    db.SaveChanges();
    transaction.Complete();
}