title: Entity Framework Core 2.0 migrations update database with seed data throwing error
body: <p>I am using EF Core 2.0 code first migration. I am trying to run the migration sin to the new database. I am using seed data. I am getting error if i use seed data and no error if i comment out the seed data method. Not sure what is the problem. Can anyone please help?</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'WorkflowBranches' in the database.</p>
  </blockquote>
  
  <p>The tables are created, but no records appear in the <code>migrationhistory</code> table, even though I have around 5 migration scripts. Also the seed data is inserted.</p>
  
  <blockquote>
    <p>I think the database is getting created twice. I am using migrations
    script and also in the seed method, there is
    dbContext.Database.EnsureCreatedAsync(). This could be the reason but
    i am not sure.</p>
  </blockquote>
  
  <p>Startup.cs:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      //Other code
      services.AddTransient&lt;DbSeeder&gt;();
  }
  
  public void Configure(IApplicationBuilder app, 
                        IHostingEnvironment env,
                        DbSeeder dbSeeder)
  {
      app.UseMvc();
  
      dbSeeder.SeedAsync(app.ApplicationServices).Wait();
  }
  </code></pre>
  
  <p>DbSeeder.cs:</p>
  
  <pre><code>using Workflow.Models;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Logging;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace Workflow.DBContext
  {
      public class DbSeeder
      {
          readonly ILogger _Logger;
  
          public DbSeeder(ILoggerFactory loggerFactory)
          {
              _Logger = loggerFactory.CreateLogger("DbSeederLogger");
          }
  
          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&lt;IServiceScopeFactory&gt;().CreateScope())
              {
                  var dbContext = serviceScope.ServiceProvider.GetService&lt;WorkflowDBContext&gt;();
                  if (await dbContext.Database.EnsureCreatedAsync())
                  {
                      if (!await dbContext.Alert.AnyAsync())
                      {
                          await InsertAlertsSampleData(dbContext);
                      }
                  }
              }
          }
  
          public async Task InsertAlertsSampleData(WorkflowDBContext dbContext)
          {
              //Insert Risktypes
              var riskTypes = GetRiskTypes();
              dbContext.RiskType.AddRange(riskTypes);
  
              try
              {
                  int numAffected = await dbContext.SaveChangesAsync();
                  _Logger.LogInformation($@"Saved {numAffected} riskTypes");
              }
              catch (Exception exp)
              {
                  _Logger.LogError($"Error in {nameof(DbSeeder)}: " + exp.Message);
                  throw;
              }
  
              //Insert categories
              var categories = GetCategories(riskTypes);
              dbContext.Category.AddRange(categories);
  
              try
              {
                  int numAffected = await dbContext.SaveChangesAsync();
                  _Logger.LogInformation($"Saved {numAffected} categories");
              }
              catch (Exception exp)
              {
                  _Logger.LogError($"Error in {nameof(DbSeeder)}: " + exp.Message);
                  throw;
              }
  
              //var alerts = GetAlerts(categories);
          }
  
          private List&lt;Alert&gt; GetAlerts(List&lt;Category&gt; categories)
          {
              return new List&lt;Alert&gt;();
          }
  
          private List&lt;RiskType&gt; GetRiskTypes()
          {
              var riskTypes = new List&lt;RiskType&gt;
              {
                  new RiskType { Name = "Risk1"},
                  new RiskType { Name = "Risk2"}
              };
  
              return riskTypes;
          }
  
          private List&lt;Category&gt; GetCategories(List&lt;RiskType&gt; riskTypes)
          {
              var categoryList = new List&lt;Category&gt;();
  
              var categories = new string[]
              {
                  "Category1Risk1",
                  "Category2Risk1",
                  "Category3Risk1",
                  "Category1Risk2"
              };
  
              //Low Risk
              foreach (var category in categories.Take(3))
              {
                  categoryList.Add(new Category
                  {
                      Name = category,
                      RiskType = riskTypes.FirstOrDefault(rt=&gt;rt.Name=='Risk1')
                  });
              }
  
              //High Risk
              foreach (var category in categories.Skip(3))
              {
                  categoryList.Add(new Category
                  {
                      Name = category,
                      RiskType = riskTypes.FirstOrDefault(rt=&gt;rt.Name=='Risk2')
                  });
              }
  
              return categoryList;
          }
      }
  }
  </code></pre>
  
  <p>Thanks</p>