ASP.Net核心类库的实体框架核心迁移

时间:2017-01-02 20:51:07

标签: asp.net-core-mvc entity-framework-core

我一直在尝试遵循Ben Cull(This is the screeshot)的建议,但数据库有点不同,因为我试图从ASP.NET Core IdentityUser类继承。我创建了一个新的解决方案,其中包含VS2015模板(CodeFirstTest)中的默认ASP.NET核心Web应用程序。然后我在解决方案中添加了一个ASP.NET Core类库(CodeFirstTest.User),该解决方案将是应用程序中的数据层,我将在其中设置ASP.NET Core Identity。

根据Ben Cull的建议,我重写了CodeFirstTest.User project.json如下。

{
  "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"
  }
}

我还创建了一个包含入口点的Program.cs文件,以及一个从ASP.NET Core IdentityUser继承的User类,如下所示。

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);
        }
    }
}

当我使用Project Manager Console创建初始​​迁移时,我收到以下错误。

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'.

我更正了第一个错误,但是第二次运行会导致dotnet失败并显示“未处理的异常”#39;错误。 问题...... 我是否编写了错误的代码来阻止迁移执行?

谢谢。

2 个答案:

答案 0 :(得分:3)

更新:现在这个答案似乎已经过时了!原来1.1已删除此功能。升级到1.1后,它不再有效。奇怪,这将停止工作。设置类库以像Ben Cull建议的那样像控制台应用程序那样工作似乎是使用EF Core 1.1时处理它的方法

那篇博客文章很老了。大多数来自.Net Core发布之前仍然通常有用的东西,需要花费很多时间。

您不再需要伪造类库中的控制台应用程序。我将一个示例身份应用程序组合在一起,其中User和DbContext位于类库中。

类库project.json看起来像这样:

{
  "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"
    }
  }
}

显示ClassLibrary命名空间和迁移过程中出现的身份模式。

Success Example

需要注意的一些细微之处:

  • 将DbContext构造函数中的选项传递给基础构造函数
  • WebApplication是启动项目。程序包管理器控制台中的默认项目是类库。这样它就知道在哪里可以找到Startup。

如果你想把它作为一个基线,那么我把Github放在一起的整个示例解决方案已经放在了measurement protocol上。

答案 1 :(得分:0)

核心1.1 使用nuget添加包后,它将无法直接工作,您还需要编辑.csproj文件

<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>

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