ASP.Net Core 类库的 Entity Framework Core 迁移

2024-01-31

我一直在尝试遵循 Ben Cull 的建议(http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects 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 失败并出现“未处理的异常”错误。 问题... 我是否编写了错误的代码,导致迁移无法执行?

谢谢。


UPDATE:现在这个答案似乎已经过时了!原来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 https://github.com/dvanherten/StackOverflow/tree/master/so-41433129如果你想用它作为基线。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.Net Core 类库的 Entity Framework Core 迁移 的相关文章

随机推荐