简体   繁体   English

LINQ to SQL默认情况下是否始终使用Transactions?

[英]Does LINQ to SQL always use Transactions by default?

I'm doing a code security analysis but i'm having some trouble to find out (in the documentation) if LINQ always use transactions when it makes the desired operation or if i have to specify some special attribute/parameter. 我正在进行代码安全性分析,但是我很难找到(在文档中)LINQ是否在进行所需操作时总是使用事务,或者我必须指定一些特殊的属性/参数。

Does anyone know this? 有谁知道这个?

LINQ, in the general case, is merely giving some query provider information about the code that defines the LINQ query. 在一般情况下,LINQ仅提供有关定义LINQ查询的代码的一些查询提供程序信息。 The query provider can do whatever it wants as its implementation. 查询提供程序可以执行它想要的任何操作。 It may use a transaction, it may not. 它可能使用交易,也可能不使用。 You could write your own query provider that wrapped every query in a transaction block if you wanted to, or you could write one that didn't. 您可以编写自己的查询提供程序,如果您愿意,可以在事务块中包装每个查询,或者您可以编写一个不包含事务块的查询。 You'd need to look through the documentation (or do some testing) for whatever specific query provider you're using (and potentially the specific query, as it may vary between queries) to see how that query provider translates the LINQ expressions into SQL. 您需要查看文档(或进行一些测试)以查找您正在使用的任何特定查询提供程序(以及可能的特定查询,因为它可能在查询之间有所不同),以查看该查询提供程序如何将LINQ表达式转换为SQL 。

The correct answer to your question is NO. 你的问题的正确答案是否定的。

With that said, you asked the wrong question. 话虽如此,你问错了问题。 LINQ is for querying data, and queries don't use transactions. LINQ用于查询数据,查询不使用事务。 Your provider that you are using under LINQ may be able to change whether it ignores, skips, or waits for locked records while querying however, but that isn't part of LINQ. 您在LINQ下使用的提供程序可能能够在查询时更改它是否忽略,跳过或等待锁定记录,但这不是LINQ的一部分。

Entity Frameworks, however, with a data provider that supports transactions will do transactions by default, which is probably what you meant to ask. 但是,实体框架与支持事务的数据提供程序将默认执行事务,这可能是您要求的。

No it is not an atomic transaction as you would see in a STORED PROCEDURE that begins with BEGIN TRANS. 不,这不是一个原子事务,就像你在以BEGIN TRANS开头的STORED PROCEDURE中看到的那样。 You would have to enlist the System.Transactions namespace and wrap your using block in a transaction. 您必须登记System.Transactions命名空间并在事务中包装您的using块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM