[英]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.