Manuel Schweigert
Manuel Schweigert

Reputation: 4974

Entity Framework on remote existing database

I am trying to upload an MVC project in which I use EF 4 Code First.

I have stumbled upon the problem that I can only create databases over the admin-console, while EF rigorously demands to create the database itself.

How can I make EF use the (already existing, but empty) database I created for it? The connectionstring is correct and it CAN connect, but I get this error:

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.

Upvotes: 1

Views: 532

Answers (2)

Corey Adler
Corey Adler

Reputation: 16149

When you have DropCreateDatabaseIfModelChanges as your initializer then the way that EF checks to see if your model has changes is to look at the Model metadata that's saved in the database and see if there are any changes. Since the database that was previously created had no such metadata in it (since it was created without EF), it had nothing to compare it to, and thus gave you that error.

To avoid this problem going forward (and without having to use the console), use a different initializer, such as EF 4.3's MigrateDatabaseToLatestVersion. I did that with an existing database for my project, and it worked just fine.

Upvotes: 1

Manuel Schweigert
Manuel Schweigert

Reputation: 4974

Okay I got it to work like this using NuGet:

PM> add-migration Initial

then I changed AutomaticMigrationsEnabled = false; in the Configuration.cs. Then:

PM> Update-Database

and it did the work without complaining. Then I ran the app again and it worked.

Upvotes: 0

Related Questions