将PENDING锁定设置为EF核心SqlLite

时间:2018-12-05 15:45:09

标签: c# sqlite asp.net-core transactionscope ef-core-2.1

我正在使用EF Core和SqlLite开发ASP .Net Core Web-api应用程序。如果有很多选择查询,则更新查询的工作速度非常慢。当查询数量增加时,出现“数据库已锁定”错误。 我认为问题出在“作家饥饿”,所以我想在DBContext将数据保存到DB时将PENDING锁设置为更新。

如何使用EF核心设置PENDING锁定?还有其他解决方案吗?

UPD:

    即使没有很多查询,有时也会发生
  1. 数据库锁定

  2. 包装:

    <PackageReference Include="Abp.AspNetCore.OData" Version="3.8.2" /> 
    <PackageReference Include="Abp.AspNetCore.SignalR" Version="3.7.2" />
    <PackageReference Include="Abp.AspNetCore" Version="3.8.2" />
    <PackageReference Include="jquery.datatables" Version="1.10.15" />
    <PackageReference Include="MailKit" Version="2.0.7" />
    <PackageReference Include="Microsoft.AspNetCore.OData" Version="7.0.1" />        
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.3" />
    <PackageReference Include="BuildBundlerMinifier" Version="2.8.391" />    
    <PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
    <PackageReference Include="NLog.MailKit" Version="3.0.0" />
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.7.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.3" />
    <PackageReference Include="Castle.Core" Version="4.3.1" />    
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.1.1" PrivateAssets="All" />
    <PackageReference Include="Abp.Dapper" Version="3.8.2" />
    <PackageReference Include="Abp.EntityFrameworkCore" Version="3.8.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
    

1 个答案:

答案 0 :(得分:0)

我认为Entity Framework没有提供任何方法来设置更新的挂起锁定,但是您可以使用data API来开始,提交和回滚事务,从而获得此行为。

您可以在更新任何事物时启动事务,并在此间隔内完成更新后提交事务,没有人将无法更新数据库中的任何数据。

Database.Begin

希望它能解决您的问题。

已更新

请注意,只有那些使用using (var context = new BloggingContext()) { using (var transaction = context.Database.BeginTransaction()) { try { //Did your work here //During this time it will help as a pending lock for other updates transaction.Commit(); } catch (Exception) { // TODO: Handle failure } } } 的另一个实例执行的查询才会被设置为挂起锁定,此行为将不适用于用于启动事务的BloggingContext()的实例。