Entity Framework 6.0: NGen.exe and Startup Performance


Native code generation occurs when the Just In time (JIT) compiler compiles the assembly's MSIL code into native code for the local machine, immediately before the method executes the first time. This is temporary code and stored in the memory that can be reclaimed by the OS at the end of process. Native code is regenerated each time when the new process is started.

The Native Image Generator NGen.exe creates a native code image using the JIT compiler and stores it in the hard drive location. Whenever an application requires the assembly, the CLR load this native image instead of the original assembly. Note that the CLR has information like which assembly has a native image and whether this image was created for debugging purposes.

A native image can improve memory use when the assembly is shared among the processes. NGen.exe can improve the startup performance of some application as a shared component already loaded for subsequent application.

Before Entity Framework 6.0, the core libraries of the EF runtime are the the part of .Net framework and native image is generated automatically. From Entity Framework version 6.0, all the EF runtime libraries are combined into an Entity Framework Nu-Get package (EntityFramework.dll and EntityFramework.SqlServer.dll), so we can generate a native image using NGen.exe.

How to use NGen.exe

Step 1

Run the Visual Studio Command prompt as an administrator.

Visual studio Command prompt as administrator

Step 2

Change the current working directory to the location of assemblies for which to generate the native image.*

Step 3

Run the following command:

ngen install <<assembly name>>

command prompt

When to use

We can consider the following assembly to create a native image.

  1. Base assembly of Entity Framework- EntityFramework.dll

    Basically an Entity Framework based application executes code from the EntityFramework.dll assembly on the startup and when it first calls the database. Hence creating a native image of this assembly helps to improve startup performance.

  2. Assembly provide by Entity Framework and used by our application

    Startup performance improvements can be also done by creating the native image of those assemblies provided by the Entity Framework and used by our application. For example EntityFramework.SqlServer.dll.

  3. Our application (project) assemblies and other dependencies

    We can create a native image of our project assemblies and other third-party dependency assemblies to improve startup performance.

Ngen.exe and Development machine

The .NET JIT compiler will offer the good overall tradeoff for code that changes very frequently during the development. We can generate native images for the compiled dependencies like the Entity Framework runtime assembly (EntityFramework.dll) and reducing time by cutting a few seconds at the beginning of each execution in the development environment.

Normally the Entity Framework runtime assemblies are located in the Nuget package location for the solution. For example if we are using Entity Framework version 6.1.1 and targeting .Net framework 4.5 then they are located under the "packages\EntityFramework.6.1.1\lib\net45" path.

To generate a native image on the deployment server we can use a third-party tool like the Wix toolkit or we can create a custom setup task to execute the ngen.exe command.

Verification: Native images are used by Entity Framework

Using the process explorer, we can verify that whether our application uses a native image by looking at loaded assemblies. Normally assemblies that have native images have an extension like "ni.dll" and "ni.exe".

Once native images are installed in the cache, we can verify it by a display command. The following command displays information for a specific assembly.

ngen display <<assembly name with path>>

command for display information for particular assembly


Native images will help us to improve the startup performance and in some cases reduce memory usage. This is not done in all scenarios and not beneficial equally. NGen.exe allows us to create a native image of assemblies that are not a part of the Global Assembly Cache (GAC). Multiple applications can share the same image that use the same Entity Framework version.