ASP.NET Core 3.1 - Migrate In Memory Database

Download KH Authenticator

KH Authenticator App Icon
.NET MAUI App for Windows and Android
Online Registration and Authentication
No Password Or Email Address Required!
Certified Providers
KenHaggerty.Com Users Without Passwords Users With Passwords Users Without Identity

This article will describe the migration of the ASP.NET Core 2.2 - Table Functions Project to the ASP.NET Core 3.1 - Table Functions Project. Visual Studio 2019 v16.4 or higher is required.

The research project I used for the articles about table functions has been upgraded to 3.1 and is published at I created a topic, ASP.NET Core 3.1 - Table Functions Project for discussions. Access to the source code may be purchased at Manage > Assets.

The article, ASP.NET Core 3.1 - Migrate From .NET Core 2.2 describes the migration of the ASP.NET Core 2.2 - Bootstrap Native Project to ASP.NET Core 3.1 which included Identity and did not use an in memory database. This migration is similar with a few important differences. The UseInMemoryDatabase option used in the Table Functions Project now requires the Microsoft. EntityFrameworkCore. InMemory NuGet package.

Edit Project File.
Edit PROJECTNAME.csproj:
Update the Target Framework:
Remove obsolete package references:
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />

Now use the Nuget Manager to install new packages. Right click on the project then click Manage NuGet Packages. The Microsoft. AspNetCore. Mvc. NewtonsoftJson package is required for proper Details serialization. See Migrate from ASP.NET Core 2.2 to 3.0 - Json.NET support. The Microsoft. EntityFrameworkCore. InMemory package is required for the in memory database.

Manage NuGet Packages.
On the Installed tab, update:
  • Microsoft. VisualStudio. Web. CodeGeneration. Design
On the Browse tab, search and install the following packages:
  • Microsoft. AspNetCore. Mvc. NewtonsoftJson
  • Microsoft. EntityFrameworkCore. InMemory
Installed NuGet Packages.

Update the Program class to use IHostBuilder. The Table Functions Project uses a DataGenerator > Initialize method after the build and before the run commands to seed the database. See ASP.NET Core 2.2 - In Memory Entities.

Edit Program.cs, implement IHostBuilder:
public static void Main(string[] args)
    //1. Get the IHost which will host this application.
    var host = CreateHostBuilder(args).Build();

    //2. Find the service layer within our scope.
    using (var scope = host.Services.CreateScope())
        //3. Get the instance of FoodDbContext in our services layer
        var services = scope.ServiceProvider;
        var context = services.GetRequiredService();

        //4. Call the DataGenerator to create sample data

    //Continue to run the application

public static IHostBuilder CreateHostBuilder(string[] args) => Host
    .ConfigureWebHostDefaults(webBuilder =>
        .ConfigureKestrel(serverOptions =>
            // Set properties and call methods on options

Update the namespaces with using Microsoft.Extensions.Hosting;.

Use Find and Replace > Current Project to replace IHostingEnvironment with IWebHostEnvironment then update the namespaces with using Microsoft.Extensions.Hosting;.

Update the configuration in the Startup class.

Edit Startup.cs > ConfigureServices:
Edit Startup.cs > Configure:
app.UseEndpoints(endpoints =>

Build, run and test.

Index Page.

Since I have been mainly using Visual Studio 2019 and the 3.x SDK, I want to share a couple of methods I have found useful.

If you have installed more than one version of Visual Studio, double clicking the solution file may open the solution in an older version. You can right click on the solution file, then Open with and select Visual Studio 2019.

Open Solution.

I set the default Opens with to 2019 by right clicking the solution file, then Properties. The Change button opens a dialog which allows you to select Visual Studio 2019.

Change Open with.

Razor pages runtime compliation was included with the Microsoft. AspNetCore. App NuGet package. To enable the runtime complilation for razor page edits without Microsoft. AspNetCore. App, you must install the Microsoft. AspNetCore. Mvc. Razor. RuntimeCompilation NuGet package and add the service to Startup.cs > ConfigureServices. I modified the .csproj file to only reference the package and the ConfigureServices to only use AddRazorRuntimeCompilation for the Debug configuration. See Razor file compilation in ASP.NET Core.

Edit <PROJECTNAME>.csproj, add Condition:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />
Then edit Startup.cs > ConfigureServices:
IMvcBuilder builder = services.AddRazorPages().AddNewtonsoftJson();
Ken Haggerty
Created 12/19/19
Updated 02/02/20 16:19 GMT

Log In or Reset Quota to read more.

Article Tags:

EF Core Model
Successfully completed. Thank you for contributing.
Contribute to enjoy content without advertisments.
Something went wrong. Please try again.



Not accepting new comments.

Submit your comment. Comments are moderated.

User Image.
DisplayedName - Member Since ?