ASP.Net核心类库的实体框架核心迁移 [英] Entity Framework Core Migration for ASP.Net Core Class Library

查看:87
本文介绍了ASP.Net核心类库的实体框架核心迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试遵循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身份.

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.

按照Ben Cull的建议,我将CodeFirstTest.User project.json重写如下.

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

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

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

当我使用Project Manager控制台创建初始迁移时,收到以下错误.

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

我更正了第一个错误,但第二次运行将导致dotnet失败,并显示未处理的异常"错误. 问题... 我是否编码不正确,从而阻止了迁移的执行?

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?

谢谢.

推荐答案

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

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

那篇博客文章很老. .Net Core发行之前的大多数内容(尽管通常仍然有用)仍然需要加一点盐.

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.

您不再需要伪造类库中的控制台应用程序.我一起整理了一个示例身份应用程序,其中User和DbContext位于类库中.

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.

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

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

显示迁移过程中遇到的ClassLibrary命名空间和身份模式.

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

一些细微的注意事项:

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

我整理的整个示例解决方案都放在 Github 如果您要将其用作基准.

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

这篇关于ASP.Net核心类库的实体框架核心迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆