无法使用实体框架教程添加迁移



我目前正忙于使用其文档页面上教程的EntityFramework部分集成Identity Server 4。我的项目使用的是.NET Core 2.0。

一切都很顺利,直到它告诉我使用命令行添加迁移。这些命令是:

dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb

第一个执行没有问题,并将迁移添加到解决方案中。使用第二个命令后,它告诉我构建失败,并出现以下错误: 1>MigrationsIdentityServerPersistedGrantDbPersistedGrantDbContextModelSnapshot.cs(13,6,13,15): error CS0579: Duplicate 'DbContext' attribute 1>MigrationsIdentityServerPersistedGrantDbPersistedGrantDbContextModelSnapshot.cs(16,33,16,43): error CS0111: Type 'PersistedGrantDbContextModelSnapshot' already defines a member called 'BuildModel' with the same parameter types 1>Done building project "IdentityServer.csproj" -- FAILED.

现在,在阅读了其他一些文章和此公告之后,(设计时)DbContext 发现似乎已更改。为了解决这个问题,我更改了一些代码。 相关部分(据我所知):

程序.cs- 扩展方法:

public static IWebHost Migrate(this IWebHost webhost)
{
using (var scope = webhost.Services.GetService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetService<PersistedGrantDbContext>().Database.Migrate();
using (var dbContext = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>())
{
dbContext.Database.Migrate();
[etc...]
}
}
}

Main(...)中调用扩展方法:

var host = BuildWebHost(args).Migrate();
host.Run();

此外,我还从Startup.cs中删除了 InitializeDatabase 方法。

完成这些操作后,在使用命令行成功添加第一个迁移后,它几乎仍然在我的解决方案中出现相同的编译错误。

我在这里错过了什么吗?

干杯。

就我而言,我发现我在/Data/Migrations 和/Migrations 上都有一个 PersistedGrantDbContextModelSnapshot.cs* 文件。 其中一项需要删除。

*显然我的名字不同。

最新更新