MVC3和code首先迁移 - "模型靠山'嗒嗒'环境已经改变,因为数据库创建" [英] MVC3 and Code First Migrations - "model backing the 'blah' context has changed since the database was created"

查看:112
本文介绍了MVC3和code首先迁移 - "模型靠山'嗒嗒'环境已经改变,因为数据库创建"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架code首先开始了我的项目。当我准备好了我上传我的数据库和code到我的主机提供商。一切工作。

I started off my project by using Entity Framework Code First. When I was ready I uploaded my database and code to my host provider. Everything worked.

我需要一个新的字段添加到我的课之一,我不想失去在数据库中的数据。因此,我尝试以下有关使用code首先迁移一些博客文章。我做了以下内容:

I need to add a new field to one of my classes and I don't want to loose the data in the database. Thus, I tried following some blog posts about using Code First Migrations. I did the following:


  1. 我支持我的遥控器(生产)数据库中。

  2. 我重视本地这个数据库

  3. 我添加的财产,以我的课

  4. PM>启用的迁移

  5. PM>添加迁移AddSortOrderToCar

  6. PM>更新 - 数据库

  7. 在这一点上我创建的本地数据库的.bak文件,然后使用该文件来恢复到远程之一。

  8. 最后,我发表了code到远程站点。

当我访问该网站,我得到了以下错误信息:
该模型靠山'blahblah'环境已经改变,因为该数据库已创建。考虑使用code首先迁移到更新数据库。

When I visit the site I get the following error message: The model backing the 'blahblah' context has changed since the database was created. Consider using Code First Migrations to update the database.

我在做什么错了?

推荐答案

从我的经验表明,迁移表不同步(即使你的数据不是),而这一直是数据库架构的一部分,现在(因为4.3我认为 - 在系统表)。

From my experience that suggests that migration table is out of sync (even if your data isn't), and that's been part of the db schema now (since 4.3 I think - under system tables).

有可能有很多的原因和方式,体验错误,​​但大部分时间...

There could be many reasons and ways to experience that error , but most of the time...

有问题的部分是手动备份/还原整个数据库code一起变化的某种组合 - 我不是,为什么总是完全肯定。

The problematic part is some combination of manually backing/restoring the full database with code changes alongside - I'm not entirely certain as to why always.

在短,即使DB-s为同一迁移表的数据可能不是 - 和散列比较可能会失败(还是满喜欢恢复不够好声音 - 但你必须'两面')。

In short, even if Db-s are the same migration table data might not be - and hash comparison may fail (still full restore sounds like good enough - but you have 'two sides').

什么对我的作品是用结果
更新,数据库-Script

What works for me is to use
Update-Database -Script

这将创建一个迁移差异,结果一个脚本
您可以手动应用为目标服务器上的数据库SQL脚本(你应该得到正确的迁移表行插入等)。

That creates a script with a 'migration difference',
which you can manually apply as an SQL script on the target server database (and you should get the right migration table rows inserted etc.).

如果仍然无法正常工作 - 你仍然可以做两件事情...

If that still doesn't work - you can still do two things...

a)将迁移表(目标 - 在系统表) - 按<一个href=\"http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx\">http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx在那里评论 - 应该故障回复到previous行为,如果你确信你的数据库-S是相同的 - 它只是要相信你,

a) remove the migration table (target - under system tables) - as per http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx comments in there - that should fail back to previous behavior and if you're certain that your Db-s are the same - it's just going to 'trust you',

B)为我所用最后一招 - 让一个更新,数据库-Script 完整的模式(例如,通过初始化一个空的数据库,应强制执行全剧本'),结果
找到 INSERT INTO [__MigrationHistory] ​​记录结果
只要运行那些,它们插入到数据库中,搜索
并确保你的数据库 - 和code匹配,

b) as a last resort I used - make a Update-Database -Script of the full schema (e.g. by initializing an empty db which should force a 'full script'),
find the INSERT INTO [__MigrationHistory] records,
just run those, insert them into the database,
and make sure that your databases - and code match,

这应使事情同步再次运行。

that should make things run in sync again.

(免责声明:这不是防弹在任何时候都工作,您可能需要尝试给当地的情景几件事情 - 但应该让你同步)

(disclaimer: this is not a bullet proof to work at all times, you may need to try a few things given your local scenarios - but should get you in sync)

这篇关于MVC3和code首先迁移 - &QUOT;模型靠山'嗒嗒'环境已经改变,因为数据库创建&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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