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.
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.
What I don’t comprehend is that I have an Initializer which implements the DropCreateDatabaseIfModelChanges
public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
and added the Initializer to the 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.
Why not 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
EdmMetadatatable. In your case that table is missing in the database. But you are using theDropCreateDatabaseIfModelChangesinitializer. Hence EF can not determine whether the model has been changed to execute the initializer.If you want to use that initializer you need to let EF create the database with the
EdmMetadatatable (meaning dropping your express database so that EF can recreate it for you from your connectionString). Otherwise remove the initializer and manually do the changes to the database or use EF migrations.