0
votes

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?

3
Could you add your startup.cs + DB context file? - suchoss
Yes, I have already added both the files. - Nimesh
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
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
Is there any specific setting in startup.cs + DB context file related to migration ? - Nimesh

3 Answers

2
votes

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.

1
votes

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
votes

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,