可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
From my integration test:
// Act Stopwatch w = new Stopwatch(); w.Start(); userService.Create(userDTO); w.Stop(); public void Create(UserDTO userDTO) { var user = userDTO.ToEntity(); _context.Entry(user).State = EntityState.Added; _context.SaveChanges(); }
6,2 seconds to do an "sql insert" is crazy. I already see the application users complaining when they first open a project which they use the whole year. So everyday they have to wait 6 seconds...
I thought the warm up time in EF6 has improved?
Is there anything I can do to improve this miserable behavior?
回答1:
Try EF6 CodeFirst View Generation T4 Template for C#. Pre-generated views improve application start-up time by moving the work that would have to be done at runtime to design time. more info
回答2:
The time is not spent to insert a simple data. EF creates the model in the memory, that is where the time you spent goes.
EF creates Entity Data Model and executes View Generation(not db views) for the first time you do an operation on context. Have a look at this blog post.
Take a look here to improve the performance by using pre-generated views to decrease model load time.
To improve the performance you can initialize your context async when you start your application. Beware the multithreading issues.
using (var context = new MyContext()) { context.Database.Initialize(false); }
回答3:
Ngen will cut that in half. Entity Framework is not compiled natively. I have a script that compiles everything in the output directory. Makes a big difference even when debugging.
@ECHO OFF REM ********************************************************************************************************* REM Compiles project's .net assemblies to native images to improve startup time and overall performance REM --------------------------------------------------------------------------------------------------------- REM Author: Brian Freeman REM History: REM 12/2/2013 Created REM ********************************************************************************************************* REM Scenarios: REM /Debug - Generate images that can be used under a debugger REM /Profile - Generate images that can be used under a profiler REM /NoDependencies - Generate the minimal number of native images REM required by this scenario REM Options REM /verbose SET DEFAULTOPTIONS= /Debug /Verbose @ECHO. ------------------------------------- @ECHO. Native Image Generator (Ngen.exe) @ECHO. ------------------------------------- @ECHO Current Defaults are %DEFAULTOPTIONS% REM --------------------------------------------------------------------- REM Small chance these might not be the locations of the .net framework REM Needs to be added to as the framework gets new versions REM --------------------------------------------------------------------- SET ngenx86=C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe SET ngenx64=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe REM Run from the current Directory to ensure any dependencies are available pushd ..\DatabaseHelper\Bin\Debug REM SKIP vshost.exe ATTRIB +S ..\*.vshost.exe /s @ECHO. ------------------------------------- @ECHO. Generator x64 Images @ECHO. ------------------------------------- for /f "delims=" %%f in ('dir *.dll /b /s /a-d-h-s') do %ngenx64% install "%%f" %* %DEFAULTOPTIONS% for /f "delims=" %%f in ('dir *.exe /b /s /a-d-h-s') do %ngenx64% install "%%f" %* %DEFAULTOPTIONS% @ECHO. ------------------------------------- @ECHO. Generator x86 Images @ECHO. ------------------------------------- for /f "delims=" %%f in ('dir *.dll /b /s /a-d-h-s') do %ngenx86% install "%%f" %* %DEFAULTOPTIONS% for /f "delims=" %%f in ('dir *.exe /b /s /a-d-h-s') do %ngenx86% install "%%f" %* %DEFAULTOPTIONS% @ECHO OFF ATTRIB -S ..\*.vshost.exe /s popd @ECHO. --------------------- @ECHO. FINISHED @ECHO. ---------------------
回答4:
Have you tried EF 6.0.2? It addresses several performance issues and may help. Find more details (including a list of bugs fixed in this release) here