繁体   English   中英

IdentityDbContext如何配置迁移

[英]IdentityDbContext how to configure migrations

IdentityDbContext如何运行迁移非常相似,但是似乎没有什么可以解决我的问题。 我已经完成了研究,关于在迁移中使用IdentityDbContext信息似乎并不多。

一些背景知识...我继承了一个使用Entity Framework的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'

因此,我已经通过NuGet将Microsoft.AspNet.Identity.Core 2.2.1安装到了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一起添加。 但是,这仅解决了Enable-Migrations问题。 在Add-Migration上仍然没有迁移配置错误。

好吧,我想我已经解决了所有问题。 因此,在Enable-Migrations工具的顶部显然会吞下丢失的引用,而不是告诉您有关引用的信息,Add-Migration工具显然需要-ConfigurationTypeName才能找到配置。 尚不完全清楚为什么,因为Enable-Migrations能够在没有该配置的情况下找到配置。 但是只要有效,这就是我所关心的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM