我一直在尝试遵循 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 失败并出现“未处理的异常”错误。
问题...
我是否编写了错误的代码,导致迁移无法执行?
谢谢。