EF .NET Core2 首次加载需要更多时间



我们开发了具有600+模型类的EF .NET Core2应用程序。那里有很多DBContext类。DB 在 AWS 环境中可用。没有可用的本地数据库。我们正在通过 webAPI 访问 EF。当我们第一次点击 API 时,加载大约需要 3 到 4 分钟。第二次开始,它会按预期在 4 秒内加载。我有什么方法可以减少加载时间。主要问题是开发人员。他们正在修复错误。每次他们在处理问题时等待 3 分钟加载。 此外,我们使用ITextsharper进行PDF工作。

我们禁用了轨道更改。

//Adding DbContext
services.AddDbContext<EPMSDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EPMSDB1")));
services.AddTransient(typeof(EPMS.Services.Authentication.IAuthenticationService), typeof(EPMS.Services.Authentication.AuthenticationService));
services.AddHttpContextAccessor();

在第一次加载期间,它必须在 30 秒内加载。

第一次速度较慢,因为 EF 正在配置表映射和上下文。这是总是会发生的事情,但是您可以通过优化dbcontext来减少它,并且有几种方法,其中一种方法是将数据库上下文拆分为具有特定实体的多个数据库上下文,这种方式对于应用程序启动来说不是那么重。

EF6

可以通过调用Initalize来预先准备实体框架。

using( var context = (...) )
{
context.Database.Initialize(force: false); 
}

EF Core

在EF Core中,DatabaseFacade不提供Initialise(),我在RelationalDatabaseFacadeExtensions中找不到这样的内容。

您仍然可以执行蠕虫查询。


加载的内容

顺便说一句。这是第一次创建DBContext在.NET Core 3.0中使用SQLite提供程序报告的内容。

using (var db = new BloggingContext())
-------------------------------------------------------------------
You may only use the Microsoft .NET Core Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software
to help you develop and test your applications.
-------------------------------------------------------------------
Loaded '...3.0.0System.Private.CoreLib.dll'. 
Loaded '...netcoreapp3.0EFGetStarted.dll'. Symbols loaded.
Loaded '...3.0.0System.Runtime.dll'. 
Loaded '...netcoreapp3.0Microsoft.EntityFrameworkCore.dll'. 
Loaded '...3.0.0netstandard.dll'. 
Loaded '...3.0.0System.ComponentModel.dll'. 
Loaded '...3.0.0System.Console.dll'. 
Loaded '...3.0.0System.Threading.dll'. 
Loaded '...3.0.0System.Runtime.Extensions.dll'. 
Loaded '...3.0.0System.Text.Encoding.Extensions.dll'. 
.NETCoreApp,Version=v3.0
Loaded '...3.0.0System.Collections.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.DependencyInjection.Abstractions.dll'. 
Loaded '...3.0.0System.Collections.Concurrent.dll'. 
Loaded '...3.0.0System.Linq.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.DependencyInjection.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Logging.Abstractions.dll'. 
Loaded '...3.0.0System.Diagnostics.DiagnosticSource.dll'. 
Loaded '...3.0.0System.Linq.Expressions.dll'. 
Loaded '...netcoreapp3.0Microsoft.EntityFrameworkCore.Abstractions.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Caching.Abstractions.dll'. 
Loaded '...3.0.0System.Resources.ResourceManager.dll'. 
Loaded '...3.0.0System.Reflection.Emit.Lightweight.dll'. 
Loaded '...3.0.0System.Diagnostics.Tracing.dll'. 
Loaded '...3.0.0System.ComponentModel.TypeConverter.dll'. 
Loaded '...3.0.0System.Reflection.Emit.ILGeneration.dll'. 
Loaded '...3.0.0System.Reflection.Primitives.dll'. 
Loaded 'Anonymously Hosted DynamicMethods Assembly'.
Loaded '...netcoreapp3.0Microsoft.EntityFrameworkCore.Sqlite.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Logging.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Options.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Logging.Console.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Logging.Configuration.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Options.ConfigurationExtensions.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Primitives.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Configuration.Abstractions.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Configuration.dll'. 
Loaded '...3.0.0System.Threading.Tasks.dll'. 
Loaded '...3.0.0System.Runtime.InteropServices.RuntimeInformation.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Configuration.Binder.dll'. 
Loaded '...3.0.0System.Threading.Thread.dll'. 
Loaded '...netcoreapp3.0Microsoft.EntityFrameworkCore.Relational.dll'. 
Loaded '...3.0.0System.Data.Common.dll'. 
Loaded '...3.0.0System.ComponentModel.Primitives.dll'. 
Loaded '...3.0.0System.Transactions.Local.dll'. 
Loaded '...3.0.0System.Runtime.InteropServices.dll'. 
dbug: Microsoft.EntityFrameworkCore.Infrastructure[10409]
An additional 'IServiceProvider' was created for internal use by Entity Framework. An existing service provider was not used due to the following configuration changes: configuration added for 'Core:UseMemoryCache', configuration added for 'Core:EnableSensitiveDataLogging', configuration added for 'Core:EnableDetailedErrors', configuration added for 'Core:ConfigureWarnings', configuration added for 'Sqlite'.
Loaded '...3.0.0System.ComponentModel.Annotations.dll'. 
Loaded '...3.0.0System.Diagnostics.Debug.dll'. 
Loaded '...3.0.0System.Private.Uri.dll'. 
Loaded '...3.0.0System.Collections.Immutable.dll'. 
Loaded '...3.0.0System.ObjectModel.dll'. 
Loaded '...netcoreapp3.0Microsoft.Extensions.Caching.Memory.dll'. 
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.0.0 initialized 'BloggingContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: None

最新更新