How to totally lock a row in Entity Framework

前端 未结 3 473
猫巷女王i
猫巷女王i 2021-02-05 12:10

I am working with a situation where we are dealing with money transactions.

For example, I have a table of users wallets, with their balance in that row.



        
3条回答
  •  星月不相逢
    2021-02-05 13:06

    EF doesn't have built-in locking mechanism, you probably would need to use raw query like

    using (var scope = new TransactionScope(...))
    {
        using (var context = new YourContext(...))
        {
            var wallet = 
                context.ExecuteStoreQuery("SELECT UserId, WalletId, Balance FROM UserWallets WITH (UPDLOCK) WHERE ...");
    
            // your logic
    
            scope.Complete();
        }
    }
    

提交回复
热议问题