1

I am getting below error while run Add-Migration InitialCreate migration command. I am using ASP.NET CORE 2.0.

An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Table 'Temp.contacts' doesn't exist Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

To resolved this error, I have tried to add a class that implements IDesignTimeDbContextFactory but doesn't work it.

Anyone knows why I got this error?

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Nimesh
  • 3,342
  • 1
  • 28
  • 35
  • Could you add your startup.cs + DB context file? – suchoss May 22 '18 at 08:54
  • Yes, I have already added both the files. – Nimesh May 22 '18 at 08:59
  • I meant here so we can see if there is something wrong. Because this error basically says that there is a problem with ApplicationDbContext. – suchoss May 22 '18 at 09:03
  • Your Startup project should be the project which is having Startup.cs file. And In package Manager console default project should be the project which is having DBContext file – Vivek Nuna May 22 '18 at 10:31
  • Is there any specific setting in startup.cs + DB context file related to migration ? – Nimesh May 22 '18 at 10:46
  • Possible duplicate of [Unable to create migrations after upgrading to ASP.NET Core 2.0](https://stackoverflow.com/questions/45782446/unable-to-create-migrations-after-upgrading-to-asp-net-core-2-0) – suchoss May 24 '18 at 10:28

3 Answers3

2

I had the same Error with table name "Users".

the reason was that I were using a seeder class and within it I were using this instruction

if (!context.Users.Any())

the problem was in it .

I commented it , added migration and then uncommented it.

Mohammed Nosirat
  • 324
  • 1
  • 3
  • 15
1

I had such error after I changed return type of CreateWebHostBuilder in Program.cs from IWebHostBuilder to IWebHost. I suppose that this method is used to add migration, so ensure to check this return type.

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseSentry();
0

I have resolved the issue by following below link.

Unable to create migrations after upgrading to ASP.NET Core 2.0

I have created another class that implements IDesignTimeDbContextFactory.

Thanks,

Nimesh
  • 3,342
  • 1
  • 28
  • 35