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

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

问题描述

我创建了一个仅代码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-a-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来执行cr如果数据库不存在或者添加种子数据,请取消该数据库。默认的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);

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

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