1

I'm trying to run the add-migration command for my database context and for some reason it's trying to do a select on a table that doesn't even exist yet (running on an empty database). The command I'm running is:

Add-Migration -context MyDbContext MyDb-1.0

I immediately get an error:

An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Invalid object name 'AccountTypes'.

Looking at the stacktrace I see the command was trying to do a select statement on a non-existing (yet) table:

fail: Microsoft.EntityFrameworkCore.Database.Command[200102]
      Failed executing DbCommand (27ms) [Parameters=[@__value_0='?'], CommandType='Text', CommandTimeout='30']
      SELECT TOP(1) [p].[Id], [p].[AccountType]
      FROM [AccountTypes] AS [p]
      WHERE [p].[Id] = @__value_0

Not sure what is going on here. I'm using ASP.NET core 2.0 with EF core 2.0. I didn't have a problem doing this with core < 2.0.

Any ideas?

Los Morales
  • 2,061
  • 7
  • 26
  • 42
  • Update: So I went ahead and manually created the AccountTypes table. Add-Migration works since it doesn't fail on the select statement. However running Update-Database fails because, like Add-Migration, it first does a select on my AccountTypes table and then tries to create the AccountTypes table... of course it's going to fail. Why is Add-Migration / Update Database commands trying to select a table? Shouldn't it just be checking to make sure it can connect to the database and then just do the create tables? – Los Morales Aug 30 '17 at 16:27
  • Try to add ToDoContextFactory: https://stackoverflow.com/questions/45782446/unable-to-create-migrations-after-upgrading-to-asp-net-core-2-0/45795315#45795315 – borisdj Aug 30 '17 at 19:39

1 Answers1

1

For those who are interested: https://github.com/aspnet/EntityFrameworkCore/issues/9625

Los Morales
  • 2,061
  • 7
  • 26
  • 42