Tom
Tom

Reputation: 4087

EF6 doesn't create any tables with code first

Requisites: ASP MVC 5 with Entity Framework 6 and MySQL.

So in visual studio 2013 i have created a new asp mvc 5 project and, with NuGet, installed the following packets:

I have followed this tutorial Getting Started with Entity Framework 6 Code First using MVC 5 :i have created the model, the context, the seed ecc... and setup the connection string to:

<add name="SchoolContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;user id=root;password=passwtest;persistsecurityinfo=True;database=dbtest"/>

Now when i try to access the Students index page i have the following exception:

{"Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations."}

I see the database and the entity framework doesn't have created any tables in the dbtest schema. Why?

Upvotes: 1

Views: 3607

Answers (1)

Steve Greene
Steve Greene

Reputation: 12304

This is almost always due to using the wrong initializer. Replace DropCreateDatabaseIfModelChanges with something else like DropCreateDatabaseAlways or CreateDatabaseIfNotExists. http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx

Upvotes: 1

Related Questions