将实体框架和SQL Server锁定对象/记录,同时将其删除 [英] Will Entity Framework and SQL Server lock a object/record while deleting it

查看:201
本文介绍了将实体框架和SQL Server锁定对象/记录,同时将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我删除使用我的asp.net MVC Web应用程序中的两个方法的一些对象和行:第一种方法包括删除实体框架的对象,如:

I am deleting some objects and rows using two methods inside my asp.net MVC web application: first approach includes deleting an Entity Framework object, such as:

public void DeleteMyObject(MyObject a)
{
    entities1.MyObject.Remove(a);
}

而第二种方法是调用从我的资源库方法的存储过程来删除数据库中的一行,例如:

while the second approach is calling a stored procedure from my repository method to delete a database row such as:

public void Deleteuserclass(string a, int u)
{
     entities1.deleteuserclass(a, u); 
}

它调用以下存储过程:

which calls the following stored procedure:

ALTER PROCEDURE dbo.deleteuserclass
    @userid nvarchar(50),
    @classid int
AS
Begin
    Delete from Users_Classes where UserID = @userid  AND ClassID = @classid

    if @@rowcount = 0     
        Raiserror('No record deleted',1,16) 
END

使用任何上述两种方法的;我可以确信,如果删除同一对象的两个删除请求在同一时间到达服务器,那么只有一个请求会从数据库中删除的记录和其他请求将收到一个异常(我的意思是将实体框架或而被删除了它的SQL Server数据库锁定该行?)?

Using any of the above two approaches; can I be confident that if two delete requests for deleting the same object arrive at the server at the same time, then only one request will delete the record from the database and the other request will receive an exception (I mean will the Entity Framework or the SQL Server database lock the row while it is being deleted ?)?

BR

推荐答案

一方或另一方会首先执行。

One or the other will execute first.

如果您的存储过程是第二execured你会得到一个例外,由于在存储过程中的if语句。

If your stored procedure is execured second you will get an exception due to the if statement in the stored procedure.

如果执行命令EF第二个会有OptimisticConcurrencyException,请参见:<一href=\"http://stackoverflow.com/questions/3695780/ef-eqivalent-for-rows-affected-of-sqlcommand-executenonquery\">EF eqivalent受影响SqlCommand.ExecuteNonQuery行

If the EF command is executed second there will be a OptimisticConcurrencyException, see: EF eqivalent for rows affected of SqlCommand.ExecuteNonQuery

这篇关于将实体框架和SQL Server锁定对象/记录,同时将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆