简体   繁体   English

Azure移动服务和EF Code First-上下文已更改错误

[英]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: 所以我很沮丧地通过以下方式修复了它:

  1. Exporting all my data out to a query window and saving the results to my local PC. 将所有数据导出到查询窗口,然后将结果保存到本地PC。
  2. Renaming the old DB 重命名旧数据库
  3. Setting AutomaticMigrationsEnabled = true; 设置AutomaticMigrationsEnabled = true; and re-deploying 并重新部署
  4. Letting the migration create the database (but this didnt create the table) 让迁移创建数据库(但这并没有创建表)
  5. Setting AutomaticMigrationDataLossAllowed = true; 设置AutomaticMigrationDataLossAllowed = true; #PanicModeOn #PanicModeOn
  6. Re-deploying 重新部署
  7. Re-importing all my data 重新导入我的所有数据

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.

相关问题 代码优先更新数据库错误“ApplicationDbContext”上下文已更改 - Code First Update Database Error 'ApplicationDbContext' context has changed EF代码率先使用SQL Azure - EF Code First to SQL Azure 首先使用EF代码连接到Azure时出现运行时错误 - Run time error while connecting to Azure using EF Code first Ef 代码第一个错误:EntityType: &#39;ModelName&#39; has no key defined - Ef code first error: EntityType: 'ModelName' has no key defined EF 6代码优先错误:EntityType没有定义键 - EF 6 Code First Error: EntityType has no key defined 手动更新数据库,支持上下文的EF complians模型已更改 - Manually updating database, EF complians model backing the context has changed 天蓝色应用服务(移动)上的Db上下文失败 - Db context on azure app service (mobile) fails 自创建数据库以来,支持“ ApplicationDbContext”上下文的模型已更改。 考虑使用代码优先迁移 - The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations EF Code首先出现1对1关系错误 - EF Code first 1 to 1 relationship error EF代码优先与关系的错误 - EF Code first Error with relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM