【发布时间】: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