There is an unsaved comment in progress. You will lose your changes if you continue. Are you sure you want to reopen the work item?

1

Closed

CreateIfNotExists w/LocalDB does not act the same as EF5

description

In my EF5 integration tests I can do a Delete Database then do a CreateIfNotExists and the LocalDB would be created and then migrated. In EF6 rc1 the same code gives me:

"Migrations is enabled for context 'Context' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console."

file attachments

comments

@ShawnC The problem with the way EF5 worked in this regard is that the CreateIfNotExists initializer would not make use of any migrations configuration or any of the scaffolded migrations when creating your database. It would instead just use all the defaults
as if you weren't using Migrations at all. This means that the database created in this way could be different from the one created using your Migrations, which could then cause errors, some of them subtle and hard to detect. This is why we prevent this from
happening in EF6.

If you want to use an initializer to create your database then looking into the MigrateDatabaseToLatestVersion initializer. This initializer integrates with your Migrations configuration such that it can create the database in the way defined by your migrations.

I can accept this answer and have created a working version based on this information. The only issue I have is if Create and CreateIfNoExists are not going to create the database then it should be marked obsolete or more information given in the intelli-sense.

@ShawnC These methods work the same as before so long as you don't have Migrations enabled. So if you're just using database initializers and not Migrations, then these methods work just as they did before. It's only when Migrations are enabled for the
context that these methods now detect this and prevent the database from being created in a potentially incorrect way.