[英]Azure Mobile Service and EF Code First - context has changed error
I have an Azure mobile service backed by a SQL database. 我有一个由SQL数据库支持的Azure移动服务。 I've been happily deploying to this for weeks and manage my DB using EF Code First.
我已经愉快地部署了数周,并使用EF Code First管理我的数据库。 Now though, I've hit a brick wall whereby any request to the mobile service breaks with an error:
现在,我遇到了麻烦,对移动服务的任何请求都会因错误而中断:
The model backing the '[yourcontext]' context has changed since the database was created.
自创建数据库以来,支持“ [您的上下文]”上下文的模型已更改。
The thing is though, it hasn't! 事实是,事实并非如此! I've tried the following:
我尝试了以下方法:
1. Re-depoyed the service *several* times
2. Run ‘Add-Migration’ to see if it mystically picks up any new fields/properties
3. Run ‘Update-database’ which runs without any issues
4. Combinations of 2&3 over and over
5. Deleted the Migration History table
6. Deleted ALL tables from my DB and re-run update-database, which again completes without error
7. 6 then 3, which recreates the database
Any ideas how I can resolve this insantiy? 有什么想法可以解决这个问题吗?
AFAIK, the Code First Migrations can be executed manually or automatically. 可以手动或自动执行代码优先迁移。 I would prefer to choose the automatic migration.
我希望选择自动迁移。 You could add the following code to
App_Start\\Startup.MobileApp.cs
file for enabling automatic migration as follows: 您可以将以下代码添加到
App_Start\\Startup.MobileApp.cs
文件中,以启用自动迁移,如下所示:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Models.MobileServiceContext, Migrations.Configuration>());
Or
var migrator = new DbMigrator(new Migrations.Configuration());
migrator.Update();
Note: You need to either configure AutomaticMigrationsEnabled to true under Migrations\\Configuration.cs
without manually adding pending model changes to a code-based migration via Add-Migration
or you could just use Add-Migration
for adding pending model changes without set AutomaticMigrationsEnabled
to true. 注意:您需要在
Migrations\\Configuration.cs
下将AutomaticMigrationsEnabled配置为true,而无需通过Add-Migration
手动将挂起的模型更改添加到基于代码Add-Migration
或者您可以使用Add-Migration
添加挂起的模型更改而无需将AutomaticMigrationsEnabled
设置为true 。
Based on your issue, I would recommend you changing the database name and use a new database to narrow this issue, also you need to remove the old migration files under the Migrations folder. 根据您的问题,建议您更改数据库名称并使用新的数据库来缩小此问题的范围,并且还需要删除“迁移”文件夹下的旧迁移文件。 Additionally, you could refer to adrian hall's book about Implementing Code First Migrations .
另外,您可以参考adrian hall的有关实现代码优先迁移的书。
So I fixed it rather frustratingly by: 所以我很沮丧地通过以下方式修复了它:
What a farce. 真是一场闹剧。 Obviously I can accommodate this now but when my app goes into production this would be a MAJOR issue!
显然,我现在可以容纳这个,但是当我的应用投入生产时,这将是一个重大问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.