MVC3和代码优先迁移 - “自从创建数据库之后,”blah“上下文的模型已经改变” [英] MVC3 and Code First Migrations - "model backing the 'blah' context has changed since the database was created"

查看:198
本文介绍了MVC3和代码优先迁移 - “自从创建数据库之后,”blah“上下文的模型已经改变”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用实体​​框架代码开始我的项目。当我准备好了,我将我的数据库和代码上传到我的主机提供商。一切正常。



我需要在我的一个类中添加一个新的字段,我不想松开数据库中的数据。因此,我尝试了一些关于使用Code First Migrations的博文。我做了以下工作:


  1. 我备份了我的远程(生产)数据库。

  2. 这个数据库本地

  3. 我将该属性添加到我的课程

  4. PM>启用迁移

  5. PM>添加迁移AddSortOrderToCar

  6. PM>更新数据库

  7. 此时,我创建了本地数据库的.bak文件,然后将该文件/>
  8. 最后,我将代码发布到远程站点。

当我访问该网站时,我收到以下错误消息:
自创建数据库以来,支持blahblah上下文的模型已更改。考虑使用代码优先迁移来更新数据库。



我做错了什么?

解决方案

根据我的经验,表明迁移表不同步(即使你的数据不是),现在也是db模式的一部分(从4.3我认为在系统表下)。



可能有很多原因和方法来体验该错误,但大部分时间...



有问题的部分是手动备份/恢复完整数据库与代码更改的组合 - 我不完全确定为什么总是。简而言之,即使Db-s是相同的迁移表数据可能不是 - 和哈希比较可能会失败(仍然完全恢复的声音听起来不错 - 但你有'双方')。






对我有用的是使用

更新数据库 - 脚本



创建具有迁移差异的脚本,

,您可以手动将其作为SQL脚本应用在目标服务器数据库(并且您应该获得正确的迁移表行插入等)。



如果仍然无效 - 您仍然可以做两件事...



a)删除迁移表(目标 - 在系统表下) - 根据 http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx 评论在那里 - 应该不会回到以前的行为,如果你确定你的Db是一样的 - 它只是要相信你,



b )作为我使用的最后手段 - 使完整模式的一个更新数据库-Script (例如,初始化一个应该强制完整脚本的空db),< br>
找到 INSERT INTO [__MigrationHistory] ​​记录,

只需运行它们,将它们插入到数据库中,

,并确保您的数据库和代码匹配,



,这应该使事情再次运行。



(免责声明:这不是任何时候都可以使用的项目符号,您可能需要根据您的本地情况尝试一些事情 - 但应该让您同步)


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.

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. I backed up my remote (production) database.
  2. I attached this database locally
  3. I added the property to my class
  4. PM> Enable-Migrations
  5. PM> Add-Migration AddSortOrderToCar
  6. PM> Update-Database
  7. At this point I created a .bak file of the local database and then used that file to 'restore' to the remote one.
  8. Lastly, I published the code to the remote site.

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.

What am I doing wrong?

解决方案

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

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.

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


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

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) 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) 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和代码优先迁移 - “自从创建数据库之后,”blah“上下文的模型已经改变”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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