EF .NET Core2首次加载花费更多时间

时间:2019-10-23 06:13:04

标签: c# entity-framework .net-core delayed-execution

我们已经开发了具有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秒内加载。

2 个答案:

答案 0 :(得分:0)

第一次是因为EF正在配置表映射和上下文,所以速度较慢。这将永远发生,但是您可以通过优化dbcontext来减少它,并且有几种方法,其中一种是将db上下文分为具有特定实体的多个db上下文,这种方式对于应用程序而言并不那么繁重开始。

答案 1 :(得分:0)

EF6

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

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

EF核心

在EF Core中,DatabaseFacade不提供Initialise(),并且在RelationalDatabaseFacadeExtensions中找不到类似的东西。

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


加载了什么

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

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.0\System.Private.CoreLib.dll'. 
Loaded '...\netcoreapp3.0\EFGetStarted.dll'. Symbols loaded.
Loaded '...\3.0.0\System.Runtime.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.EntityFrameworkCore.dll'. 
Loaded '...\3.0.0\netstandard.dll'. 
Loaded '...\3.0.0\System.ComponentModel.dll'. 
Loaded '...\3.0.0\System.Console.dll'. 
Loaded '...\3.0.0\System.Threading.dll'. 
Loaded '...\3.0.0\System.Runtime.Extensions.dll'. 
Loaded '...\3.0.0\System.Text.Encoding.Extensions.dll'. 
.NETCoreApp,Version=v3.0
Loaded '...\3.0.0\System.Collections.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll'. 
Loaded '...\3.0.0\System.Collections.Concurrent.dll'. 
Loaded '...\3.0.0\System.Linq.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.DependencyInjection.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Logging.Abstractions.dll'. 
Loaded '...\3.0.0\System.Diagnostics.DiagnosticSource.dll'. 
Loaded '...\3.0.0\System.Linq.Expressions.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.EntityFrameworkCore.Abstractions.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Caching.Abstractions.dll'. 
Loaded '...\3.0.0\System.Resources.ResourceManager.dll'. 
Loaded '...\3.0.0\System.Reflection.Emit.Lightweight.dll'. 
Loaded '...\3.0.0\System.Diagnostics.Tracing.dll'. 
Loaded '...\3.0.0\System.ComponentModel.TypeConverter.dll'. 
Loaded '...\3.0.0\System.Reflection.Emit.ILGeneration.dll'. 
Loaded '...\3.0.0\System.Reflection.Primitives.dll'. 
Loaded 'Anonymously Hosted DynamicMethods Assembly'.
Loaded '...\netcoreapp3.0\Microsoft.EntityFrameworkCore.Sqlite.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Logging.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Options.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Logging.Console.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Logging.Configuration.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Options.ConfigurationExtensions.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Primitives.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Configuration.Abstractions.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Configuration.dll'. 
Loaded '...\3.0.0\System.Threading.Tasks.dll'. 
Loaded '...\3.0.0\System.Runtime.InteropServices.RuntimeInformation.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.Extensions.Configuration.Binder.dll'. 
Loaded '...\3.0.0\System.Threading.Thread.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.EntityFrameworkCore.Relational.dll'. 
Loaded '...\3.0.0\System.Data.Common.dll'. 
Loaded '...\3.0.0\System.ComponentModel.Primitives.dll'. 
Loaded '...\3.0.0\System.Transactions.Local.dll'. 
Loaded '...\3.0.0\System.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.0\System.ComponentModel.Annotations.dll'. 
Loaded '...\3.0.0\System.Diagnostics.Debug.dll'. 
Loaded '...\3.0.0\System.Private.Uri.dll'. 
Loaded '...\3.0.0\System.Collections.Immutable.dll'. 
Loaded '...\3.0.0\System.ObjectModel.dll'. 
Loaded '...\netcoreapp3.0\Microsoft.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