本文介绍了EF代码优先-无法检查模型兼容性,因为数据库不包含模型元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已启用自动迁移.然后,我删除了整个数据库.接下来,我从命令控制台执行了Update-database,它重新创建了我的数据库.然后,我启动我的应用程序仅看到此错误:

I have enabled automatic migrations. Then, I deleted my whole db. Next, i executed Update-database from command console, and it recreated my db. Then, I started my application only to see this error:

那元数据到底是什么,我该如何指向它呢?

So what exactly is that metadata, and how can I point entity framework to it?

PS.我的数据库包含名为MigrationsHistory的表.

PS. My database contains table named MigrationsHistory.

推荐答案

这是我前一段时间写的解决此问题的可能方法的详细说明...
(不完全是您所经历的,因此本身并不是重复的,而是要考虑不同的情况)

Here is a detailed description of the possible ways to resolve this which I wrote a while ago...
(not exactly what you're experiencing, hence not a duplicate per se, but with different scenarios in mind)

https://stackoverflow.com/a/10255051/417747

总结...

这会创建一个带有迁移差异"的脚本,您可以 在目标服务器数据库上手动将其作为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)删除迁移表(目标-在系统表下)-按照 http: //blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx 那里的评论-应该会退回到以前的行为,如果 您确定您的Db-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)作为我最后使用的方法-创建一个Update-Database -Script 完整模式(例如,通过初始化一个空的db来强制执行 '完整脚本'),找到INSERT INTO [__MigrationHistory]记录, 只需运行它们,将它们插入数据库,并确保您的 数据库-和代码匹配,

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.

如果有帮助

这篇关于EF代码优先-无法检查模型兼容性,因为数据库不包含模型元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 22:25