实体框架code仅错误:该模型支持的背景已经改变,因为在创建数据库 [英] Entity Framework Code Only error: the model backing the context has changed since the database was created

查看:138
本文介绍了实体框架code仅错误:该模型支持的背景已经改变,因为在创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个code只有POCO反对使用实体框架4和CTP4现有的数据库使用。当我运行一个查询,我得到的错误

模型支持的xyzCont​​ext背景已经改变,因为在创建数据库。无论是手动删除/更新数据库,或拨打Database.SetInitializer与IDatabaseInitializer实例。例如,RecreateDatabaseIfModelChanges战略将自动删除并重新创建数据库,以及可选的新数据的种子吧。

我不清楚为什么会这样或者是我可以改变。我只是创造了POCO,定义了一个简单的DbContext,做了一些调整,然后再尝试运行一个简单的查询。由于我使用的是code只,我不知道需要进行任何配置设置。我当然不希望重新创建或删除数据库,因为它是一个现有的数据库。

感谢您的任何想法。

解决方案

我发现在这个职位上斯科特Guthrie的博客的评论答案。

<一个href="http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-$c$c-first-with-an-existing-database.aspx">http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-$c$c-first-with-an-existing-database.aspx

  

对于那些谁看到这个异常:

     

的模式支持生产方面已经改变了自创建数据库。手动删除/更新数据库,或拨打Database.SetInitializer与IDatabaseInitializer实例。

     

下面是什么,是怎么回事,该怎么办呢:

     

当第一次创建一个模型,我们运行一个DatabaseInitializer做事情喜欢创建数据库,如果它不存在或添加种子数据。默认DatabaseInitializer尝试比较使用该模型与存储在一个与数据库中创建一个EdmMetadata表的模式的散列所需的数据库模式(当c首先$ C $是一个创建数据库)。现有的数据库将不会有EdmMetadata表等不会有散......而今天的实现将抛出,如果该表丢失。我们将努力在改变这个行为之前,我们的船fial版本,因为它是默认的。在此之前,现有的数据库通常不需要任何数据库初始化,因此它可以通过调用关闭的上下文类型:

  Database.SetInitializer&lt;制造&GT;(NULL);
 

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

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.

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.

Thanks for any ideas.

解决方案

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:

"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:

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);

这篇关于实体框架code仅错误:该模型支持的背景已经改变,因为在创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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