Entity Framework Core Migration for ASP.Net Core Class Library

asp.net-core-mvc entity-framework-core

Question

I've been trying to follow the advice of Ben Cull (http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects), but the database is a little different in that I'm trying to inherit from ASP.NET Core IdentityUser class. I created a new solution containing the default ASP.NET Core Web Application from the VS2015 template (CodeFirstTest). I then added an ASP.NET Core class-library (CodeFirstTest.User) to the solution, which would be the data layer in the application and where I will also be setting up ASP.NET Core Identity.

Following the advice of Ben Cull, I rewrote the CodeFirstTest.User project.json as follows.

{
  "version": "1.0.0-*",

  "buildOptions": {
    "emitEntryPoint": true
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.Extensions.Configuration": "1.0.1",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore.Design": "1.0.1",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  }
}

I also created a Program.cs file containing the entry point, and a User class that inherits from ASP.NET Core IdentityUser as shown below.

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace CodeFirstTest.User
{
    public class Program
    {
        public static void Main(string[] args) { }
    }

    public class User : IdentityUser
    {

    }

    public class UserIdentityDbContext : IdentityDbContext<User>
    {
        public UserIdentityDbContext(DbContextOptions options)
        {

        }
    }

    public class TemporaryDbContextFactory : IDbContextFactory<UserIdentityDbContext>
    {
        public UserIdentityDbContext Create(DbContextFactoryOptions options)
        {
            var builder = new DbContextOptionsBuilder<UserIdentityDbContext>();
            builder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=UserDb;Trusted_Connection=SqlTruncateException;MultipleActiveResultSets=true");
            return new UserIdentityDbContext(builder.Options);
        }
    }
}

When I use the Project Manager Console to create the initial migration, I received the following errors.

PM> Add-Migration -Name "Initial" -Project "CodeFirstTest.User"
Could not invoke this command with the startup project 'CodeFirstTest'. Check that 'Microsoft.EntityFrameworkCore.Design' has been added to "dependencies" in the startup project and that the version of 'Microsoft.EntityFrameworkCore.Tools' in "tools" and 'Microsoft.EntityFrameworkCore.Design' are the same. See http://go.microsoft.com/fwlink/?LinkId=798221 for more details.
PM> Add-Migration -Name "Initial" -Project "CodeFirstTest.User"
Unhandled Exception: System.MissingMethodException: Entry point not found in assembly 'Microsoft.EntityFrameworkCore.Design, Version=1.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

I corrected the first error, but the second run will cause dotnet to fail with the 'Unhandled Exception' error. The question... Am I coding something incorrectly that prevents the migration from executing?

Thank you.

1
4
1/2/2017 8:51:07 PM

Accepted Answer

UPDATE: And now this answer appears to be out of date! Turns out 1.1 has removed this feature. After upgrade to 1.1 it no longer works. Odd that this would stop working. Setting up the class library to work like a console application as Ben Cull suggests appears to be the way to handle it when using EF Core 1.1


That blog article is pretty old. Most things that are from before the .Net Core release while still usually useful, need to be taken with a grain of salt.

You no longer have to fake out a console app in the class library. I whipped together a sample identity application where the User and DbContext were in a class library.

The class library project.json looked like this:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Microsoft.EntityFrameworkCore": "1.0.1",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

To show the ClassLibrary namespace and the identity schema coming through in the migration.

Success Example

A few subtle things to note:

  • Passed the options in the DbContext constructor to the base constructor
  • WebApplication was the startup project. The default project in the Package Manager Console was the Class Library. This is so that it knows where to find Startup.

The whole example solution I put together has been put up on Github if you want to use it as a baseline.

3
2/10/2017 2:27:08 AM

Popular Answer

for core 1.1 after adding the packages with nuget it will not work directly, you will need also to edit the .csproj file like that

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
  </ItemGroup>

</Project>

reference: https://www.benday.com/2017/02/14/walkthrough-entity-framework-core-1-1-with-migrations/



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow