Search code examples
entity-framework.net-corevisual-studio-mac

Data Seeding by HasData not working on Visual Studio for Mac


I am getting some wonky behavior in Visual Studio for MacOS. I recently installed .NET Core 2.1 SDK and then upgraded a project from 2.0 to 2.1. I wanted to try out the new Data Seeding feature, which lets us seed data into an entity in an override of OnModelCreating method.

modelBuilder.Entity<Role>().HasData(
            new Role { ID = 1, Name = "Administrator", RoleType = 0 },
            new Role { ID = 2, Name = "Application User", RoleType = 0 });

Initially, that will build. Then I open a command line and type the command:

dotnet ef migration add v001

The result is "build failed". Then when I go into VS and build, it fails. I have restarted VS and can repeat this over and over. Initially, the project builds without error. Then only after attempting to add the EF migration, the build error appears. The code completion does show the HasData method and I seem to be using it properly. I suspect I am hitting a tooling bug here and appreciate any recommendations on how to get around this.


Solution

  • The problem was that I had not followed a migration step that is required when migrating from .NET Core 2.0 to 2.1:

    Replace the version specified "Microsoft.AspNetCore.All" package reference with the versionless "Microsoft.AspNetCore.App" package reference. You may need to add dependencies that were removed from "Microsoft.AspNetCore.All"

    This is documented here:

    https://learn.microsoft.com/en-us/aspnet/core/migration/20_21?view=aspnetcore-2.1