第一个实体框架6非查询的六秒预热时间

时间:2013-12-13 11:42:22

标签: entity-framework entity-framework-6

从我的集成测试:

// 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 insert”很疯狂。我已经看到应用程序用户在第一次打开他们使用全年的项目时抱怨。所以他们每天都要等6秒......

我认为EF6的预热时间有所改善?

我能做些什么来改善这种悲惨的行为吗?

4 个答案:

答案 0 :(得分:5)

不花时间插入简单数据。 EF在内存中创建模型,即您花费的时间。

EF首次对上下文执行操作时,会创建实体数据模型并执行“视图生成”(而不是数据库视图)。看看this blog post

通过使用预生成的视图来减少模型加载时间,看看here以提高性能。

要提高性能,您可以在启动应用程序时初始化上下文异步。注意多线程问题。

using (var context = new MyContext())
{
    context.Database.Initialize(false);
}

答案 1 :(得分:3)

Ngen将减少一半。实体框架不是本机编译的。我有一个脚本编译输出目录中的所有内容。即使在调试时也会产生很大的不同。

    @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. ---------------------

答案 2 :(得分:2)

试试EF6 CodeFirst View Generation T4 Template for C#。预生成的视图通过将必须在运行时完成的工作移动到设计时来改善应用程序启动时间。 more info

答案 3 :(得分:0)

你试过EF 6.0.2吗?它解决了几个性能问题并可能有所帮助。查找更多详细信息(包括此版本中修复的错误列表)here