简体   繁体   English

将数据库从嵌入式更改为SQL Server Express后,Entity Framework 4.1代码第一个模型兼容性问题

[英]Entity Framework 4.1 code first model compatibility issue after changing database from embedded to SQL Server Express

Hello Entity Frameworks Gurus!! 你好实体框架专家!

I've been following the official tutorial and have started a small project with it. 我一直在关注官方教程,并开始了一个小型项目。 I've started using SQL Server Compact Edition and have decided to change it to a SQL Server Express database. 我已经开始使用SQL Server Compact Edition,并决定将其更改为SQL Server Express数据库。

After changing my connectionstring 更改我的连接字符串后

 <add name="SchoolContext" connectionString="Data  Source=|DataDirectory|Registration.sdf" providerName="System.Data.SqlServerCe.4.0"/>
 <!--    <add name="SchoolContext" connectionString="Data  Source=.\SQLEXPRESS;Integrated Security=True;Initial  Catalog=SchoolRegistration;MultipleActiveResultSets=True"  providerName="System.Data.SqlClient"/>-->

it started throwing this error: 它开始抛出此错误:

Model compatibility cannot be checked because the database does not contain model metadata. 由于数据库不包含模型元数据,因此无法检查模型兼容性。 Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions. 确保已将IncludeMetadataConvention添加到DbModelBuilder约定中。

What I don't comprehend is that I have an Initializer which implements the DropCreateDatabaseIfModelChanges 我不理解的是我有一个实现DropCreateDatabaseIfModelChanges的初始化DropCreateDatabaseIfModelChanges

 public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>

and added the Initializer to the Global.asax.cs application_start() 并将初始化器添加到Global.asax.cs application_start()

 Database.SetInitializer<SchoolContext>(new SchoolInitializer());

So after my error I deleted the SQL Server Compact .sdf file and switched back to its connection string and all the changes and all fixtures where put in it. 因此,在发生错误之后,我删除了SQL Server Compact .sdf文件,并切换回其连接字符串以及所有更改和放入其中的所有固定装置。

Why not SQL Server Express? 为什么不使用SQL Server Express?

Something I am missing? 我缺少什么? Or should I have created another context for the new connection string and refactor my Initializer? 还是应该为新的连接字符串创建另一个上下文并重构初始化程序? Thanks for reading this 感谢您阅读本

EF checks whether the model and the database are in sync by checking the values in the EdmMetadata table. EF通过检查EdmMetadata表中的值来检查模型和数据库是否同步。 In your case that table is missing in the database. 您的情况是该表在数据库中丢失。 But you are using the DropCreateDatabaseIfModelChanges initializer. 但是,您正在使用DropCreateDatabaseIfModelChanges初始化程序。 Hence EF can not determine whether the model has been changed to execute the initializer. 因此,EF无法确定模型是否已更改为执行初始化程序。

If you want to use that initializer you need to let EF create the database with the EdmMetadata table (meaning dropping your express database so that EF can recreate it for you from your connectionString). 如果要使用该初始化程序,则需要让EF使用EdmMetadata表创建数据库(这意味着删除您的express数据库,以便EF可以从connectionString为您重新创建它)。 Otherwise remove the initializer and manually do the changes to the database or use EF migrations. 否则,请删除初始化程序,然后手动对数据库进行更改或使用EF迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架4.1不会向SQL Server Express数据库添加任何行 - Entity Framework 4.1 is not adding any rows to SQL Server Express database 实体框架6.0代码优先迁移-模型/数据库兼容性错误? - Entity Framework 6.0 Code First Migration - Model/Database compatibility bug? 实体框架4.1代码优先EDMX问题 - Entity Framework 4.1 Code First EDMX issue 实体框架4.1禁用模型兼容性检查 - Entity Framework 4.1 Disable Model Compatibility Check 实体框架4.1代码优先和自动映射器问题 - entity framework 4.1 code first and auto mapper issue 实体框架4.1 RC:Code First EntityTypeConfiguration继承问题 - Entity Framework 4.1 RC: Code First EntityTypeConfiguration inheritance issue 在现有MySQL数据库上使用实体框架4.1 Code-First - Using entity framework 4.1 Code-First on existing MySQL database 实体框架4.1代码数据库中的第一个和永久表 - Entity Framework 4.1 Code First and permanent table in database 实体框架:如何在“代码优先”迁移中禁用模型兼容性检查 - Entity Framework: How to disable model compatibility check in Code First Migrations 如何使用实体框架生成与SQL Server兼容的数据库(代码优先) - How to generate a SQL Server compatible database with Entity Framework (code first)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM