ASP.NET Core 3.1 - Migrate In Memory Database


Ken Haggerty
Created 12/19/2019 - Updated 02/02/2020 16:19

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 tables.kenhaggerty.com. 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:
<TargetFramework>netcoreapp3.1</TargetFramework>
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)
{
    //CreateHostBuilder(args).Build().Run();
    //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
        DataGenerator.Initialize(services);
    }

    //Continue to run the application
    host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) => Host
    .CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder
        .ConfigureKestrel(serverOptions =>
        {
            // Set properties and call methods on options
        })
        .UseStartup<Startup>();
    });

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:
Remove:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
Add:
services.AddRazorPages().AddNewtonsoftJson();
Edit Startup.cs > Configure:
Remove:
app.UseMvc();
Add:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
});

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:
//services.AddRazorPages().AddNewtonsoftJson();
IMvcBuilder builder = services.AddRazorPages().AddNewtonsoftJson();
#if DEBUG
    builder.AddRazorRuntimeCompilation();
#endif

Article Tags:

Table Functions

Comment Count = 0

Please log in to comment or follow.

Login Register
Follow to get web notifications when new comments are posted to this article.
Logged in users receive web notifications for new articles, topics and assets.
Web Notifications