从我的集成测试来看:
// 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秒来做一个"sql插入“是疯狂的。我已经看到应用程序用户在他们第一次打开他们全年使用的项目时抱怨。所以他们每天都要等6秒.
我以为EF6的热身时间已经改善了?
我能做些什么来改善这种悲惨的行为吗?
发布于 2013-12-13 18:19:09
试试EF6 CodeFirst视图生成T4模板的C#。预生成的视图通过移动运行时必须完成的工作来改进应用程序启动时间。更多信息
发布于 2013-12-13 11:51:30
没有花费时间插入一个简单的数据。EF在内存中创建模型,这就是您花费的时间。
EF创建实体数据模型并执行视图生成(而不是db视图),这是您第一次对上下文进行操作。看看这篇博客文章。
查看一下这里,通过使用预生成的视图来减少模型加载时间来提高性能。
要提高性能,可以在启动应用程序时初始化上下文异步。小心多线程问题。
using (var context = new MyContext())
{
context.Database.Initialize(false);
}发布于 2014-01-18 03:06:41
恩根会把它切成两半。实体框架不是本地编译的。我有一个脚本,它编译输出目录中的所有内容。即使在调试时也会产生很大的影响。
@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. ---------------------https://stackoverflow.com/questions/20565679
复制相似问题