ASP.NET Core and Entity Framework 6

Note

This isn't the latest version of this article. For the current release, see the .NET 8 version of this article.

By Patrick Goode

Using Entity Framework 6 with ASP.NET Core

Entity Framework Core should be used for new development. The download sample uses Entity Framework 6 (EF6), which can be used to migrate existing apps to ASP.NET Core.

Additional resources

By Paweł Grudzień and Damien Pontifex

This article shows how to use Entity Framework 6 in an ASP.NET Core application.

Overview

To use Entity Framework 6, your project has to compile against .NET Framework, as Entity Framework 6 doesn't support .NET Core. If you need cross-platform features you will need to upgrade to Entity Framework Core.

The recommended way to use Entity Framework 6 in an ASP.NET Core application is to put the EF6 context and model classes in a class library project that targets .NET Framework. Add a reference to the class library from the ASP.NET Core project. See the sample Visual Studio solution with EF6 and ASP.NET Core projects.

You can't put an EF6 context in an ASP.NET Core project because .NET Core projects don't support all of the functionality that EF6 commands such as Enable-Migrations require.

Regardless of project type in which you locate your EF6 context, only EF6 command-line tools work with an EF6 context. For example, Scaffold-DbContext is only available in Entity Framework Core. If you need to do reverse engineering of a database into an EF6 model, see Code First to an Existing Database.

Reference full framework and EF6 in the ASP.NET Core project

Your ASP.NET Core project needs to target .NET Framework and reference EF6. For example, the .csproj file of your ASP.NET Core project will look similar to the following example (only relevant parts of the file are shown).

<PropertyGroup>
  <TargetFramework>net452</TargetFramework>
  <PreserveCompilationContext>true</PreserveCompilationContext>
  <AssemblyName>MVCCore</AssemblyName>
  <OutputType>Exe</OutputType>
  <PackageId>MVCCore</PackageId>
</PropertyGroup>

When creating a new project, use the ASP.NET Core Web Application (.NET Framework) template.

Handle connection strings

The EF6 command-line tools that you'll use in the EF6 class library project require a default constructor so they can instantiate the context. But you'll probably want to specify the connection string to use in the ASP.NET Core project, in which case your context constructor must have a parameter that lets you pass in the connection string. Here's an example.

public class SchoolContext : DbContext
{
    public SchoolContext(string connString) : base(connString)
    {
    }

Since your EF6 context doesn't have a parameterless constructor, your EF6 project has to provide an implementation of IDbContextFactory<TContext>. The EF6 command-line tools will find and use that implementation so they can instantiate the context. Here's an example.

public class SchoolContextFactory : IDbContextFactory<SchoolContext>
{
    public SchoolContext Create()
    {
        return new EF6.SchoolContext("Server=(localdb)\\mssqllocaldb;Database=EF6MVCCore;Trusted_Connection=True;MultipleActiveResultSets=true");
    }
}

In this sample code, the IDbContextFactory implementation passes in a hard-coded connection string. This is the connection string that the command-line tools will use. You'll want to implement a strategy to ensure that the class library uses the same connection string that the calling application uses. For example, you could get the value from an environment variable in both projects.

Set up dependency injection in the ASP.NET Core project

In the Core project's Startup.cs file, set up the EF6 context for dependency injection (DI) in ConfigureServices. EF context objects should be scoped for a per-request lifetime.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
    services.AddScoped<SchoolContext>(_ => new SchoolContext(Configuration.GetConnectionString("DefaultConnection")));
}

You can then get an instance of the context in your controllers by using DI. The code is similar to what you'd write for an EF Core context:

public class StudentsController : Controller
{
    private readonly SchoolContext _context;

    public StudentsController(SchoolContext context)
    {
        _context = context;
    }

Sample application

For a working sample application, see the sample Visual Studio solution that accompanies this article.

This sample can be created from scratch by the following steps in Visual Studio:

  • Create a solution.

  • Add > New Project > Web > ASP.NET Core Web Application

    • In project template selection dialog, select API and .NET Framework in dropdown
  • Add > New Project > Windows Desktop > Class Library (.NET Framework)

  • In Package Manager Console (PMC) for both projects, run the command Install-Package Entityframework.

  • In the class library project, create data model classes and a context class, and an implementation of IDbContextFactory.

  • In PMC for the class library project, run the commands Enable-Migrations and Add-Migration Initial. If you have set the ASP.NET Core project as the startup project, add -StartupProjectName EF6 to these commands.

  • In the Core project, add a project reference to the class library project.

  • In the Core project, in Startup.cs, register the context for DI.

  • In the Core project, in appsettings.json, add the connection string.

  • In the Core project, add a controller and view(s) to verify that you can read and write data. (Note that ASP.NET Core MVC scaffolding won't work with the EF6 context referenced from the class library.)