2017-11-30 9 views
0

update-databaseコマンドを実行せずにEF移行がシードデータと共に実行されるかどうかを知りたいですか?すなわち、 update-databaseコマンドを実行せずに、データベースに作成されたすべてのテーブルを見ることができます。Entityframework Core 2.0アプリケーションの初回実行時に移行が実行されますか?

問題はありますか、これは予期された動作ですか?

私はデータをシードする別の方法を使用しています:

public async Task SeedAsync(IServiceProvider serviceProvider) 
    { 
     //Based on EF team's example at https://github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/Models/SampleData.cs 
     using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope()) 
     { 
      var dbContext = serviceScope.ServiceProvider.GetService<WorkflowDBContext>(); 
      if(!dbContext.AllMigrationsApplied()) 
      { 
       serviceScope.ServiceProvider.GetService<WorkflowDBContext>().Database.Migrate(); 
       if (!await dbContext.Alert.AnyAsync()) 
       { 
        await SeedData(dbContext); 
       } 
      } 
     } 
    } 

public static class DbContextExtension 
{ 
    public static bool AllMigrationsApplied(this WorkflowDBContext context) 
    { 
     var applied = context.GetService<IHistoryRepository>() 
          .GetAppliedMigrations() 
          .Select(m => m.MigrationId); 

     var total = context.GetService<IMigrationsAssembly>() 
          .Migrations 
          .Select(m => m.Key); 

     return !total.Except(applied).Any(); 
    } 
} 

おかげ

答えて

0

EFコアが今まで自動的にマイグレーションを適用されません。あなたのアプリケーションはDbContext.Database.EnsureCreated()またはMigrate()のどこかに電話している可能性があります。

+0

ありがとうございます。私は質問を更新し、データをシードするために使用しているコードを貼り付けました。私がやったことは、これで正しいかコードのにおいですか? –

関連する問題