简体   繁体   English

IdentityDbContext如何配置迁移

[英]IdentityDbContext how to configure migrations

Very similar to IdentityDbContext how to run migrations , but nothing there seemed to solve my issues. IdentityDbContext如何运行迁移非常相似,但是似乎没有什么可以解决我的问题。 I've done my research, and there doesn't seem to be much info out there on using IdentityDbContext with migrations. 我已经完成了研究,关于在迁移中使用IdentityDbContext信息似乎并不多。

Some background... I inherited a web app that uses Entity Framework (which I'm not really that familiar with). 一些背景知识...我继承了一个使用Entity Framework的Web应用程序(我不太熟悉)。 I wasn't sure what libraries the packages.config was missing. 我不确定packages.config缺少哪些库。 I eventually got that sorted and found it used Entity Framework. 我最终得到了排序,发现它使用了实体框架。 I currently have it set to use 6.2.0 and the app seems to work fine. 我目前已将其设置为使用6.2.0,并且该应用似乎正常运行。

The original developer had implemented IdentityDbContext and was using automatic migrations. 最初的开发人员已经实现了IdentityDbContext并且正在使用自动迁移。 He also had a DbMigrationsConfiguration implementation already defined, which I've added some seed queries to. 他还已经定义了一个DbMigrationsConfiguration实现,我在其中添加了一些种子查询。

I've been using the automatic migrations so far, but I know it's not recommended for production. 到目前为止,我一直在使用自动迁移,但是我知道不建议将其用于生产。 And I've had a couple times where I had to enable data loss to get the app to deploy, so that's probably why I shouldn't be using it. 而且我有几次不得不丢失数据才能部署该应用程序,所以这可能就是为什么我不应该使用它的原因。

This led me to Code First Migrations with an existing database , specifically to set up migrations for an existing schema. 这使我开始使用现有数据库进行代码优先迁移 ,特别是为现有模式设置迁移。 That's where I'm lost. 那就是我迷路的地方。 It doesn't seem like any of those instructions work for IdentityDbContext . 这些指令似乎都不适合IdentityDbContext Here's what I've tried: 这是我尝试过的:

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'.

So, this led me to questioning whether IdentityDbContext would even work. 因此,这使我开始质疑IdentityDbContext是否还可以工作。 So, next I tried changing my context to inherit from DbContext , which also meant having to add DbSet properties for users and roles so that my code would compile (though I never tested to see whether the app still worked). 因此,接下来,我尝试将上下文更改为继承自DbContext ,这也意味着必须为用户和角色添加DbSet属性,以便我的代码可以编译(尽管我从未测试过该应用程序是否仍然有效)。 Now, here's what I tried: 现在,这是我尝试的方法:

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).

Am I reading this right? 我读对了吗? It can find a migrations configuration, but then it can't? 它可以找到迁移配置,但是找不到吗? Does anyone test these tools? 有人测试过这些工具吗?

Ok, so then I decide to take the recommendation of using -Force: 好的,因此我决定接受使用-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.

And, as it says, the project failed to build because my existing DbMigrationsConfiguration implementation got overwritten. 而且,正如它所说,该项目无法构建,因为我现有的DbMigrationsConfiguration实现被覆盖。 My existing implementation had a constructor that took some data for the seed queries. 我现有的实现有一个构造函数,该构造函数为种子查询获取了一些数据。 So, just for testing I decided not to worry about those seed queries, so I kept the auto-generated configuration and updated my app code to compensate. 因此,仅出于测试目的,我决定不担心那些种子查询,因此保留了自动生成的配置并更新了我的应用程序代码以进行补偿。 Now on to creating the initial migration: 现在开始创建初始迁移:

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'

So, I already had Microsoft.AspNet.Identity.Core 2.2.1 installed to DataLayer through NuGet, but apparently it wasn't added as a reference in the DataLayer project. 因此,我已经通过NuGet将Microsoft.AspNet.Identity.Core 2.2.1安装到了DataLayer ,但是显然没有在DataLayer项目中将其添加为参考。 Added it in and let's try yet again: 添加了它,让我们再试一次:

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.

I assume these errors have to do with me switching IdentityDbContext to DbContext . 我认为这些错误与我将IdentityDbContext切换到DbContext

I don't really know where to go from here and I'm kind of frustrated. 我真的不知道从这里去哪里,我有点沮丧。 It really doesn't seem like this is going to work. 看来这似乎行不通。 If I really do have to change my IdentityDbContext over to a DbContext every time I add a migration, that's just unacceptable. 如果确实必须在每次添加迁移时都必须将IdentityDbContext更改为DbContext ,那是不可接受的。

Even if I did get this to work, it seems convoluted when it's easy to write straight SQL DDL scripts. 即使我确实做到了这一点,但在编写直接的SQL DDL脚本很容易时,却似乎有些费解。 I suppose the advantage is migrations cover multiple versions of upgrades, but that can be handled with DDL scripts and a version number table, so that's not really an issue either. 我想这样做的好处是迁移可以涵盖多个版本的升级,但是可以使用DDL脚本和版本号表来处理,因此这也不是真正的问题。

Should I just try to give up using migrations? 我应该只是放弃使用迁移吗? Or maybe Entity Framework completely? 还是完全是实体框架?

Edit: 编辑:

I found No context type was found in the assembly EF6 which explained why IdentityDbContext wasn't working. 我发现在程序集EF6中未找到任何上下文类型,这解释了为什么IdentityDbContext无法正常工作。 Apparently Microsoft.AspNet.Identity.Core doesn't get added as a reference, which seemed odd because I was kind of expecting the reference to be added with NuGet. 显然,没有添加Microsoft.AspNet.Identity.Core作为参考,这似乎很奇怪,因为我有点希望将参考与NuGet一起添加。 But, that only solves the Enable-Migrations issue. 但是,这仅解决了Enable-Migrations问题。 Still has the no migration configuration error on Add-Migration. 在Add-Migration上仍然没有迁移配置错误。

Well, I think I have it all fixed. 好吧,我想我已经解决了所有问题。 So, on top of the Enable-Migrations tool apparently swallowing missing references instead of telling you about them, the Add-Migration tool apparently needs -ConfigurationTypeName in order to find the configuration. 因此,在Enable-Migrations工具的顶部显然会吞下丢失的引用,而不是告诉您有关引用的信息,Add-Migration工具显然需要-ConfigurationTypeName才能找到配置。 Not entirely sure why, since Enable-Migrations was able to find the configuration without this. 尚不完全清楚为什么,因为Enable-Migrations能够在没有该配置的情况下找到配置。 But as long as it works, that's all I care about. 但是只要有效,这就是我所关心的。

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

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