【问题标题】:Entity Framework Core Migration for ASP.Net Core Class LibraryASP.Net Core 类库的 Entity Framework Core 迁移
【发布时间】:2017-05-16 22:22:46
【问题描述】:

我一直在尝试遵循 Ben Cull (http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects) 的建议,但数据库有点不同,因为我尝试从 ASP.NET Core IdentityUser 类继承。我创建了一个新的解决方案,其中包含来自 VS2015 模板 (CodeFirstTest) 的默认 ASP.NET Core 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);
        }
    }
}

当我使用项目管理器控制台创建初始迁移时,我收到以下错误。

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 失败并出现“未处理的异常”错误。 问题... 我是否编码错误导致迁移无法执行?

谢谢。

【问题讨论】:

  • 我刚刚看了看,看起来您仍然需要将您的类库转换为应用程序(至少在 VS2017 和工具正确发布之前)。我目前使用的是 CLI 工具(dotnet ef 迁移)而不是 powershell 工具,但两者的工作原理应该基本相同。小心你的版本,只有特定的组合才能正常工作。
  • 查看下面 Dave 答案中的版本,并使用他的 Github 项目作为参考。让我知道他们是否确实有效:)
  • 我相信这可能是关键。我相信我记得 CLI 工具对我不起作用,除非我做了应用程序变通办法,但是当他(和我)使用包管理器控制台时,我没有考虑这个事实,我宁愿不必这样做。我也在使用 VS 2015 Community 作为示例。

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


【解决方案1】:

更新:现在这个答案似乎已经过时了!原来 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 命名空间和身份架构。

需要注意的一些微妙的事情:

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

如果您想将其用作基线,我整理的整个示例解决方案已放在Github 上。

【讨论】:

  • 首先,我很抱歉,因为我在另一个项目上被拖了 29 天。其次,通过检查发布的启动项目,我能够完成初始迁移。我进行了两项更改以完成迁移。问题中列出的代码的第一个更改是
  • 对上面问题中列出的代码的第一个更改是 `public UserIdentityDbContext(DbContextOptions options) : base(options) { }` 我还添加了 "CodeFirstTest.User":"1.0. 0-*" 到 CodeFirstTest 项目的依赖项。
  • 几个问题,1)为了真正的关注点分离,难道DbContext初始化也需要移到类库项目吗?在您的代码中,它仍在网络应用程序中。 ( ) .AddDefaultTokenProviders();
  • 在本示例中,您通常会做很多我没有做的事情。这纯粹是一个概念证明,迁移可以存在并在类库中创建。出于该上下文的目的,不需要连接任何东西。
【解决方案2】:

对于核心 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/

【讨论】:

    猜你喜欢
    • 2019-02-12
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2020-03-09
    • 2017-02-06
    相关资源
    最近更新 更多