1

Please do not mistake this SO question as a duplicate:

I have the following config for a custom database initializer for code first entity framework:

<entityFramework>
  <contexts>
    <context type="MyApp.Context, MyApp">
      <databaseInitializer type="MyApp.CustomInitializer, MyApp" />
    </context>
  </contexts>
</entityFramework>

When I run the application all is well and the InitializeDatabase(Context context) method is called as expected.

However, when running the command Update-Database in the Package manager console it is not using my custom initializer.

Why does it work when running the app but not from the package manager console? Which initializer will it be using?

Community
  • 1
  • 1
Andy Clark
  • 3,363
  • 7
  • 27
  • 42
  • Is that config in the same project used for `Update-Database`, either as the default project in the console or selected by `-ProjectName` or `-StartUpProjectName`? – jjj May 07 '15 at 16:24
  • It is not in the same project where `Update-Database` is used. it is in the start up project. I assumed this config only existing in the start up project would suffice since the command takes the connection string from the start up... – Andy Clark May 08 '15 at 08:33
  • I have now added the above config to the project containing the migrations but has made no difference. The migrations still run using a different initializer – Andy Clark May 08 '15 at 08:43
  • What does `Update-Database` need the initializer for? – jjj May 08 '15 at 22:58
  • There's a `-ConfigurationTypeName` option if that's what you're looking for. – jjj May 08 '15 at 23:04
  • @jjj I have customized my initialiser which performs additional maintenance after each migration. – Andy Clark May 10 '15 at 11:22

2 Answers2

0

Unfortunately, Update-Database does not use a database initializer at all. It creates a DbMigrator to handle the update (which is also what the MigrateDatabaseToLatestVersion initializer uses), and either tries to find a DbMigrationsConfiguration class or uses one that you provide on the command line.

If you need to do some maintenance after a migration, you can use the Up() function in the migration itself. Or I suppose you could the Seed() function in your class that overrides DbMigrationsConfiguration (but by default, Seed() runs for every database initialization...however, there are ways to work around this.)

If you want to see it at work: https://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Migrations/Design/ToolingFacade.cs

Community
  • 1
  • 1
jjj
  • 4,822
  • 1
  • 16
  • 39
-1

If you call Update-Database without specifying where is the ConfigurationType it is assumed that it is in the same assembly. An example :

Update-Database -ProjectName MyApp.Migrations -ConfigurationTypeName "MyApp.Migrations.MyMigrationConfiguration" -ConnectionString "Data Source=.\SQLEXPRESS;Database=myAppDb;Trusted_Connection=False;User ID=xx;Password=xxxxxx" -ConnectionProviderName "System.Data.SqlClient"
Ognyan Dimitrov
  • 6,026
  • 1
  • 48
  • 70