IdentityDbContext 如何配置迁移



与IdentityDbContext如何运行迁移非常相似,但似乎没有什么能解决我的问题。我已经完成了研究,似乎没有太多关于将IdentityDbContext与迁移一起使用的信息。

一些背景...我继承了一个使用实体框架的 Web 应用程序(我不太熟悉(。我不确定packages.config缺少哪些库。我最终得到了排序,发现它使用了实体框架。我目前将其设置为使用 6.2.0,该应用程序似乎工作正常。

原始开发人员已实现IdentityDbContext并使用自动迁移。他还已经定义了一个DbMigrationsConfiguration实现,我在其中添加了一些种子查询。

到目前为止,我一直在使用自动迁移,但我知道不建议将其用于生产。而且我有几次必须启用数据丢失才能部署应用程序,所以这可能就是我不应该使用它的原因。

这促使我开始使用现有数据库进行代码优先迁移,特别是为现有架构设置迁移。这就是我迷路的地方。这些说明似乎都不适用于IdentityDbContext.这是我尝试过的:

PM> Enable-Migrations
No context type was found in the assembly 'WebApp'.
PM> Enable-Migrations -ProjectName DataLayer
No context type was found in the assembly 'DataLayer'.

所以,这让我质疑IdentityDbContext是否有效。因此,接下来我尝试更改上下文以继承DbContext,这也意味着必须为用户和角色添加DbSet属性,以便我的代码可以编译(尽管我从未测试过该应用程序是否仍然有效(。现在,这是我尝试过的:

PM> Enable-Migrations -ProjectName DataLayer
Migrations have already been enabled in project 'DataLayer'. To overwrite 
the existing migrations configuration, use the -Force parameter.
PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
No migrations configuration type was found in the assembly 'DataLayer'. (In 
Visual Studio you can use the Enable-Migrations command from Package Manager 
Console to add a migrations configuration).

我没看错吗?它可以找到迁移配置,但找不到?有人测试这些工具吗?

好的,那么我决定采用使用 -Force 的建议:

PM> Enable-Migrations -ProjectName DataLayer -Force
Code First Migrations enabled for project DataLayer.
PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
The project 'WebApp' failed to build.

而且,正如它所说,该项目未能构建,因为我现有的DbMigrationsConfiguration实现被覆盖了。我现有的实现有一个构造函数,它为种子查询获取一些数据。因此,仅出于测试,我决定不担心这些种子查询,因此我保留了自动生成的配置并更新了我的应用程序代码以进行补偿。现在开始创建初始迁移:

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
System.IO.FileNotFoundException: Could not load file or assembly 
'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system 
cannot find the file specified.
File name: 'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=31bf3856ad364e35'

因此,我已经安装了Microsoft.AspNet.Identity.Core 2.2.1以通过NuGet进行DataLayer,但显然它没有作为参考添加到DataLayer项目中。添加它,让我们再试一次:

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more 
validation errors were detected during model generation:
DataLayer.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key 
defined. Define the key for this EntityType.
DataLayer.IdentityUserRole: : EntityType 'IdentityUserRole' has no key 
defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on 
type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on 
type 'IdentityUserRole' that has no keys defined.

我认为这些错误与我将IdentityDbContext切换到DbContext有关。

我真的不知道该何去何从,我有点沮丧。这似乎真的行不通。如果我真的必须在每次添加迁移时将IdentityDbContext更改为DbContext,那是不可接受的。

即使我确实让它工作,当编写直接的SQL DDL脚本很容易时,它似乎很复杂。我想优点是迁移涵盖了多个版本的升级,但这可以通过 DDL 脚本和版本号表来处理,所以这也不是真正的问题。

我应该尝试放弃使用迁移吗?或者也许完全是实体框架?

编辑:

我发现在程序集EF6中找不到上下文类型,这解释了为什么IdentityDbContext不起作用。显然Microsoft.AspNet.Identity.Core没有被添加为引用,这看起来很奇怪,因为我有点期待使用NuGet添加引用。但是,这只能解决启用迁移问题。在添加迁移上仍然存在无迁移配置错误。

好吧,我想我已经解决了所有问题。因此,除了启用迁移工具显然吞噬丢失的引用而不是告诉您有关它们的信息之外,添加迁移工具显然需要 -ConfigurationTypeName 才能找到配置。不完全确定原因,因为启用迁移能够在没有此配置的情况下找到配置。但只要它有效,这就是我所关心的。

最新更新