Entity Framework Code Only 错误:自创建数据库以来支持上下文的模型已更改 [英] Entity Framework Code Only error: the model backing the context has changed since the database was created

查看:24
本文介绍了Entity Framework Code Only 错误:自创建数据库以来支持上下文的模型已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个仅代码"POCO,用于使用实体框架 4 和 CTP4 的现有数据库.当我运行查询时出现错误

I created a "Code Only" POCO for use against an existing database using Entity Framework 4 and the CTP4. When I run a query I get the error

自数据库创建以来,支持xyzContext"上下文的模型已更改.手动删除/更新数据库,或使用 IDatabaseInitializer 实例调用 Database.SetInitializer.例如,RecreateDatabaseIfModelChanges 策略将自动删除并重新创建数据库,并可选择使用新数据为其设定种子.

The model backing the 'xyzContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the RecreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

我不清楚为什么会发生这种情况或我可以改变什么.我只是创建了 POCO,定义了一个简单的 DbContext,做了一些调整,然后尝试运行一个简单的查询.由于我使用的是仅代码",因此我不知道需要进行任何配置设置.而且我当然不想重新创建或删除数据库,因为它是一个现有的数据库.

I'm unclear on why this is happening or what I can change. I merely created the POCO, defined a simple DbContext, made a few tweaks, and then tried to run a simple query. Since I'm using "Code Only", I'm unaware of any configuration settings that need to be made. And I certainly don't want to recreate or delete the database since it's an existing database.

感谢您的任何想法.

推荐答案

我在 Scott Guthrie 博客上这篇帖子的评论中找到了答案.

I found the answer in the comments on this post on Scott Guthrie's blog.

http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx

对于那些看到此异常的人:

For those who are seeing this exception:

"自数据库创建以来,支持生产"上下文的模型已更改.手动删除/更新数据库,或使用 IDatabaseInitializer 实例调用 Database.SetInitializer."

"The model backing the 'Production' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance."

这是正在发生的事情以及如何处理它:

Here is what is going on and what to do about it:

首次创建模型时,我们会运行 DatabaseInitializer 以执行诸如创建数据库(如果数据库不存在)或添加种子数据之类的操作.默认的 DatabaseInitializer 尝试将使用模型所需的数据库架构与存储在使用数据库创建的 EdmMetadata 表中的架构的散列进行比较(当 Code First 是创建数据库的时候).现有数据库不会有 EdmMetadata 表,因此不会有哈希……如果该表丢失,今天的实现将抛出.我们将在发布最终版本之前更改此行为,因为它是默认版本.在此之前,现有数据库通常不需要任何数据库初始值设定项,因此可以通过调用为您的上下文类型关闭它:

When a model is first created, we run a DatabaseInitializer to do things like create the database if it's not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). Existing databases won’t have the EdmMetadata table and so won’t have the hash…and the implementation today will throw if that table is missing. We'll work on changing this behavior before we ship the fial version since it is the default. Until then, existing databases do not generally need any database initializer so it can be turned off for your context type by calling:

Database.SetInitializer<Production>(null);

这篇关于Entity Framework Code Only 错误:自创建数据库以来支持上下文的模型已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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