ChiliYago
ChiliYago

Reputation: 12279

Configuring DropCreateDatabaseAlways

I have been successfully using the following config in my app.config file to set the Entity Framework initialization strategy.

<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[My.DataLayer.MyModelContext, My.DataLayer], [My.DataLayer.Migrations.Configuration, My.DataLayer]], EntityFramework"></databaseInitializer>

Now I wanted to change it to DropCreateDatabaseAlways but I keep getting an errors when calling update-database or when running the program.

<databaseInitializer type="System.Data.Entity.DropCreateDatabaseAlways`2[[My.DataLayer.MyModelContext, My.DataLayer], [My.DataLayer.Migrations.Configuration, My.DataLayer]], EntityFramework"></databaseInitializer>

The syntax is exactly the same in with the exception of DropCreateDatabaseAlways vs. MigrateDatabaseToLatestVersion.

Error:

Failed to set database initializer of type 'System.Data.Entity.DropCreateDatabaseAlways`2[[My.DataLayer.MyModelContext, My.DataLayer], [My.DataLayer.Migrations.Configuration, My.DataLayer]], EntityFramework' for DbContext type 'My.DataLayer.MyModelContext, My.DataLayer' specified in the application configuration. See inner exception for details.

Upvotes: 1

Views: 2668

Answers (1)

nemesv
nemesv

Reputation: 139748

With the help of the `2 notation you can specify generic arguments.

You need to write `2 because the MigrateDatabaseToLatestVersion class has two generic arguments.

But the DropCreateDatabaseAlways only accepts one generic parameter TContext baseAlways<TContext> : IDatabaseInitializer<TContext>

So you need to writte

<databaseInitializer type="System.Data.Entity.DropCreateDatabaseAlways`1[[My.DataLayer.MyModelContext, My.DataLayer]], EntityFramework"></databaseInitializer>

Upvotes: 3

Related Questions