简体   繁体   English

EF Core 3.1 在 DbContext 中设置默认 IsolationLevel

[英]EF Core 3.1 set default IsolationLevel in DbContext

I need to apply the same IsolationLevel to all the operation executed by the DbContext, so I don't have to specify it each time I use it.我需要将相同的 IsolationLevel 应用于 DbContext 执行的所有操作,因此我不必每次使用时都指定它。 Is there any way to do it?有什么办法吗?

I'm working with EF Core 3.1 and SqlServer我正在使用 EF Core 3.1 和 SqlServer

UPDATE : after some research and tests I found out that what I'm looking for is to apply WITH (NOLOCK) to the tables.更新:经过一些研究和测试,我发现我正在寻找的是将WITH (NOLOCK)应用于表。 Also I tried to apply the transaction scope to a single query and tried to read data from a locked table and it is not working.此外,我尝试将事务 scope 应用于单个查询,并尝试从锁定的表中读取数据,但它不起作用。

This is the code I used:这是我使用的代码:

using var transactionScope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted });

// query execution

transactionScope.Complete();

This code have been copied from: https://docs.microsoft.com/en-gb/ef/core/saving/transactions , the only difference is on the IsolationLevel.此代码已复制自: https://docs.microsoft.com/en-gb/ef/core/saving/transactions ,唯一的区别在于 IsolationLevel。

https://stackoverflow.com/a/53082098/12919581 https://stackoverflow.com/a/53082098/12919581

I have this solution that is the best I could found, even if in my opinion is not good enought.我有这个我能找到的最好的解决方案,即使在我看来还不够好。

It generates this warning that could create some problems for future update.它会生成此警告,可能会为将来的更新带来一些问题。

Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQuerySqlGenerator is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.

I'm not adopting it, but at the moment it is the only way I could found to reach the goal of reading uncommitted data without locking any table.我没有采用它,但目前这是我能找到的在不锁定任何表的情况下达到读取未提交数据的目标的唯一方法。

EF Core is an ORM that can run against multiple data sources, not all of them changing transaction isolation level, because of this, in EF this property is readonly. EF Core 是一个 ORM 可以针对多个数据源运行,并非所有数据源都会更改事务隔离级别,因此,在 EF 中此属性是只读的。 If you want to change it for SQL Server, you will need to do it from within your T-Sql code.如果您想为 SQL 服务器更改它,您需要在您的 T-Sql 代码中进行。

You should do it inside the chain-of-responsibility of Dotnet core middlewares.您应该在 Dotnet 核心中间件的责任链中执行此操作。 before each action execution, you can start a transaction scope and set transaction isolation level and, end it after action execution.每次action执行前,可以启动一个事务scope并设置事务隔离级别,在action执行后结束。 See middlewares and dotnet pipelines here:在此处查看中间件和 dotnet 管道:

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
       app.Use(async (context, next) =>
       {
           //begin your transaction scope.
           await next.Invoke();
           //finalize the corresponding scope.
       });
    }
 }

this pattern is very similar to unit-of-work implementation.这种模式与工作单元实现非常相似。

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

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