问题描述
我有两个用于迁移的文件夹(AuthContext和UserProfileContext),每个都有自己的迁移和一些自定义的sql,然后在运行这些迁移以进行数据迁移等等。
I have two folders for my migrations (AuthContext and UserProfileContext), each has their own migration and some custom sql to run afterwards for data migrations and whatnot.
这使用程序包管理器控制台时,工作正常。我
This works fine when using package manager console. I
- 从生产中恢复
- 运行Update-Database -ConfigurationTypeName Migrations.Auth.Configuration
- 运行Update-Database -ConfigurationTypeName Migrations.UserProfile.Configuration
然后,
我尝试通过以下方式测试发布部分的迁移:
I tried to test out the migrations on publish piece by:
- 在dev数据库上恢复生产
- 指向开发数据库的单个连接字符串(所有上下文都使用相同的字符串)
- 发布到azure网站
- 选中应用代码优先迁移框
- 选择该单个连接字符串
- Restore production on dev database
- Single connection string (all contexts use the same) pointed to dev database
- Publish to azure web site
- Checked the box for Apply Code First Migrations
- Selected that single connection string
好的,它发表得很好;但是,当我查看数据库时,什么也没发生!它没有创建必要的表,列或数据移动。
Okay it published fine; however, when I went to look at the database, nothing happened! It did not create the necessary tables, columns, or data moves.
更新1 我尝试过的
以下内容的任意组合:仅一个连接字符串,因此我想这不是问题,并检查了执行迁移。
Update 1I've tried any combination of the below: only one single connection string so I'm guessing that's not the issue, and execute migrations is checked.
在发布api运行,但不进行数据库更改。我以为也许我需要先打它,但是当我尝试使用api时(我现在当然依赖于新的数据库设置)却得到了随机错误,并且数据库仍然没有更改。
On publish the api runs but no database changes are made. I thought perhaps I needed to hit it first but I just get random errors when I try to use the api (which now of course relies on the new database setup), and the database is still not changed.
我已经看到一些参考,其中提到需要向我的Startup类中添加一些内容,但是我不确定如何继续。
I've seen a couple references out there about needing to add something to my Startup class but I'm not sure how to proceed.
更新2
我通过在连接字符串中添加 Persist Security Info = True解决了一个问题。现在,它实际上已连接到数据库并调用了我的API;但是,没有任何迁移在运行。
我将调试器附加到Azure开发环境并逐步执行...在我的第一个数据库上,它进入了有问题的Migration的Configuration类,然后进入barfs,我无法跟踪错误。
Update 2I solved one issue by added "Persist Security Info=True" to my connection string. Now it actually connects to the database and calls my API; however, no migrations are running.I attached debugger to Azure dev environment and stepped through... on my first database call it steps into the Configuration class for the Migration in question, then barfs and I can't track down the error.
public Configuration()
{
AutomaticMigrationsEnabled = false;
MigrationsDirectory = @"Migrations\Auth";
ContextKey = "AuthContext";
}
更新3
好吧,挖掘一下,它第一次到达数据库时我们就在出错。是的,这是有道理的,因为模型已更改,但是我已经进行了迁移,启用并检查了迁移!同样,当从程序包管理器控制台运行 Update-Database时,它运行良好,但在发布到Azure期间使用执行代码优先迁移时却不能正常运行
Okay, dug down and the first time it hits the database we're erroring. Yes this makes sense since the model has changed, but I have migrations in place, enabled, and checked! Again, it works fine when running "Update-Database" from package manager console, but not when using Execute Code First Migrations during publish to Azure
更新4
好吧,我在这里找到了根本问题。 VS仅在我的一个数据库上下文中为databaseInitializer设置了额外的web.config属性,实际上是我的应用程序首先击中了未提及的那个。
Update 4Okay I found the root issue here. VS is setting up the additional web.config attrib for databaseInitializer on only one of my database contexts, the one not mentioned is in fact hit first from my app.
我必须弄清楚如何使其包含多个上下文,或者将我所有的内容组合到一个上下文中。
So now I have to figure out how to get it to include multiple contexts, or, combine all of my stuff into a single context.
推荐答案
解决!总结后代的解决方案:
Solved! To summarize the solution for posterity:
启用代码优先迁移后,每个复选框仅针对一个基本连接字符串启用它们,而不管针对该基本连接字符串进行了多少次上下文迁移。因此,在我的情况下,我将有问题的两个细分为两个不同的连接字符串。
Enable Code First Migrations only enables them for one base connection string per checkbox checked, regardless of how many contexts have migrations against that base connection string. So in my case I broke out the two in question into two different connection strings.
然后我遇到了其他错误,并确定是否要更改基本连接字符串到模型支持asp身份,您需要包括(一次发布)附加标志base( AuthContext,throwIfV1Schema:false)
Then I was hitting other errors and identified that if you're changing the base connection string to the model backing asp identity you need to include (one time publish) the additional flag base("AuthContext" , throwIfV1Schema: false)
这篇关于部署到Azure后未运行EF代码首次迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!