IdentityDbContext如何配置迁移 [英] IdentityDbContext how to configure migrations

查看:78
本文介绍了IdentityDbContext如何配置迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IdentityDbContext如何运行迁移非常相似,但似乎没有任何解决方法我的问题。我已经完成了研究,并且在迁移中使用 IdentityDbContext 似乎没有很多信息。

Very similar to IdentityDbContext how to run migrations, but nothing there seemed to solve my issues. I've done my research, and there doesn't seem to be much info out there on using IdentityDbContext with migrations.

某些背景...我继承了一个使用Entity Framework的Web应用程序(我并不那么熟悉)。我不确定package.config中缺少哪些库。我最终得到了排序,发现它使用了实体框架。我目前已将其设置为使用6.2.0,并且该应用程序似乎正常运行。

Some background... I inherited a web app that uses Entity Framework (which I'm not really that familiar with). I wasn't sure what libraries the packages.config was missing. 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.

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

The original developer had implemented IdentityDbContext and was using automatic migrations. He also had a DbMigrationsConfiguration implementation already defined, which I've added some seed queries to.

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

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.

带我去代码优先迁移现有数据库,专门用于设置现有架构的迁移。那就是我迷路的地方。这些指示似乎都不适合 IdentityDbContext 。这是我尝试过的方法:

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

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

So, this led me to questioning whether IdentityDbContext would even work. 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). 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).

我读对了吗?它可以找到迁移配置,但是找不到吗?有人测试过这些工具吗?

好吧,所以我决定接受使用-Force的建议:

Ok, so then I decide to take the recommendation of using -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 的实现被覆盖。我现有的实现有一个构造函数,该构造函数为种子查询获取了一些数据。因此,仅出于测试目的,我决定不担心那些种子查询,因此我保留了自动生成的配置并更新了我的应用程序代码以进行补偿。现在开始创建初始迁移:

And, as it says, the project failed to build because my existing DbMigrationsConfiguration implementation got overwritten. 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'

因此,我已经将 Microsoft.AspNet.Identity.Core 2.2.1 安装到 DataLayer 通过NuGet,但是显然它没有作为 DataLayer 项目中的引用添加。将其添加到其中,然后再试一次:

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

我认为这些错误与我切换 IdentityDbContext DbContext

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.

即使我确实做到了这一点,在编写简单的SQL DDL脚本时也似乎有些费解。我想这样做的好处是迁移可以涵盖多个版本的升级,但是可以使用DDL脚本和版本号表来处理,因此也不是问题。

Even if I did get this to work, it seems convoluted when it's easy to write straight SQL DDL scripts. 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.

我应该只是尝试放弃使用迁移吗?还是完全是Entity Framework?

编辑:

I找到在程序集EF6中未找到上下文类型这就解释了为什么IdentityDbContext无法正常工作。显然没有添加Microsoft.AspNet.Identity.Core作为参考,这似乎很奇怪,因为我有点希望NuGet会添加参考。但是,这仅解决了Enable-Migrations问题。在Add-Migration上仍然没有迁移配置错误。

I found No context type was found in the assembly EF6 which explained why IdentityDbContext wasn't working. 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. But, that only solves the Enable-Migrations issue. Still has the no migration configuration error on Add-Migration.

推荐答案

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

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. Not entirely sure why, since Enable-Migrations was able to find the configuration without this. But as long as it works, that's all I care about.

这篇关于IdentityDbContext如何配置迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆