EF code首先认为数据库是过时的 - 只有在生产 [英] EF Code First thinks database is out of date -- only in production

查看:338
本文介绍了EF code首先认为数据库是过时的 - 只有在生产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 3应用程序正在使用SQL Server CE 4.0和Entity Framework 4.1 code首先。 它工作正常,在当地,但在生产时,出现以下错误:

I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0 and Entity Framework 4.1 Code First. It works fine locally, but in production, it fails with the following error:

该模式支持的'foobar的'环境已经改变,因为该数据库已创建。无论是手动删除/更新数据库,或致电Database.SetInitializer与IDatabaseInitializer实例。例如,DropCreateDatabaseIfModelChanges战略将自动删除并重新创建数据库,以及可选的新的数据种子吧。

The model backing the 'foobar' 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 DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

我已经验证了我的应用程序的DLL和数据库文件均达到最新的远程服务器上,但我不断地得到这个错误。

I've verified that my application dll and database file are up-to-date on the remote server, but I continue to get this error.

任何想法,问题可能是什么?

Any idea what the problem could be?

请注意:我使用的是实体框架4.1的RTF版本,而不是CTP

Note: I'm using the RTF version of the Entity Framework 4.1, not CTP.

修改

好吧,如果我删除EdmMetadata表,并重新发布数据库到生产服务器,我不再得到的错误。不过,我完全不明白为什么我应该这样做。该模型的没有改变的。它在数周内没有变化,我只是今天重建数据库。我想关键就在这整个事情是哈希值。什么是EdmMetadata散列对被比较?

Okay, if I delete the EdmMetadata table, and re-publish the database to the production server, I no longer get the error. However, I completely don't understand why I should have to do this. The model has not changed. It hasn't changed in weeks, and I rebuilt the database just today. I guess the key to this whole thing is the hash. What is the EdmMetadata hash being compared against?

编辑2

据的标记S的回答,在EdmMetadata表中的散列对SSDL的哈希值进行比较。然而,没有实际SSDL文件(扩展.ssdl和含有SSDL没有文件没有文件)中的溶液中的任何地方。那么,哪里是SSDL实际存储?它是在设计时或运行时产生的?

According to Mark S's answer, the hash in the EdmMetadata table is compared against a hash of the SSDL. However, there is no actual SSDL file (no file with extension .ssdl and no file containing "ssdl") anywhere in the solution. So, where is the SSDL actually stored? Is it generated at design time or run time?

修改3

要回答我的问题,SSDL的确实的获得产生,并在运行时的缓存(更多详情的这里),但看着SSDL的实际模式,我不知道我看到任何可能改变,只是因为该项目的DLL生活在不同的服务器上的不同位置。

To answer my own question, SSDL does get generated and cached at runtime (more details here), but looking at the actual schema of SSDL, I'm not sure I see anything that could possibly change simply because the project's DLL lives in a different location on a different server.

请注意:我使用的是完全相同的数据库文件(。自卫队),用于测试和生产。我从字面上FTP-ING从斌/的App_Data文件到App_Data文件夹在生产服务器上。我也FTP-ING项目的DLL。所以,这应该是测试和生产之间唯一不同的是,这些文件都在不同的地方。

Note: I'm using the exact same database file (.sdf) for test and production. I'm literally ftp-ing the file from bin/App_Data to the App_Data folder on the production server. I'm also ftp-ing the project DLL. So, the only thing that should be different between test and production is that these files live in a different place.

此外,为了记录,这里是我的连接字符串:

Also, just for the record, here is my connection string:

的DataSource = | DataDirectory目录| foobar.sdf的providerName =System.Data.SqlServerCe.4.0

修改4(最终修改?)

问题竟然是一个DLL的问题。我认为,不同版本的DLL的EntityFramework的正在远程使用与本地。解决的办法是,以确保我有最新版本的EF 4.1(RTW),清并重新添加引用,重新生成数据库,并重新发布。一切现在是伟大的工作。

The problem turned out to be a DLL issue. I think a different version of the EntityFramework DLL was being used remotely vs. locally. The solution was to make sure I had the latest version of EF 4.1 (RTW), clear and re-add references, regenerate the database, and re-publish. Everything is now working great.

推荐答案

在回答什么是EdmMetadata散列对被比较?

In response to "What is the EdmMetadata hash being compared against?"

该EdmMetadata表允许code首先要确定是否在您的code中的模式是相同的数据库模型。该表格​​包含一个行这是您的SSDL的哈希值。 SSDL表示存储架构定义语言,是用来描述一个实体模型一种基于XML的语言和模型之间的映射。

The EdmMetadata table allows Code First to determine if the model in your code is the same as the model in your database. The table contains a single row which is a hash of your SSDL. SSDL stands for Store Schema Definition Language and is an XML based language used to describe an Entity model and the mappings between models.

您可以阅读更多关于SSDL和查看在规范<一个href=\"http://msdn.microsoft.com/en-us/library/bb399559.aspx\">http://msdn.microsoft.com/en-us/library/bb399559.aspx

You can read more about SSDL and view the specification at http://msdn.microsoft.com/en-us/library/bb399559.aspx

您提到的东西制作精细,但在当地无法工作。你让code首先建立本地和生产数据库?是生产数据库中已存在的或另一种方式所产生?

You mention that things work fine locally but not in production. Are you allowing Code First to build both the local and production databases? Is the production database already existing or being generated in another fashion?

codeFirst报道称,在数据库中的哈希值不匹配,present模型的散列这样的东西必须是不同的。搞清楚什么可能是一个挑战。

CodeFirst is reporting that the hash in the database doesn't match the present model's hash so something must be different. Figuring out what may be a challenge.

的EdmMetadata表被映射到EdmMetadata类。有一个在这个类,你可以用它来获得哈希值的特定环境下一个TrygetModelHash方法。如果你绝望,你可以用它来您当前的模型比较了在你的生产数据库的哈希值。这将至少让你不必持续不断地建立一个新的数据库只是用于测试。

The EdmMetadata table is mapped to the EdmMetadata class. There is a TrygetModelHash method in this class which you can use to get the hash for a given context. If you're desparate you could use this to compare your current model to the hash that's in your production database. This would at least keep you from having to build a new database constantly just for testing.

您还可以指示code如果需要,首先要排除EdmMetadata表。

You can also instruct Code First to exclude the EdmMetadata table if desired.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}

这篇关于EF code首先认为数据库是过时的 - 只有在生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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