No databases found in the project during publishing of website

Viewed 2769

I am trying to publish an MVC Core project with Entity Framework Core from within Visual Studio 2017 Community, but VS cannot find any project that contains the DbContext and it therefore says: No databases found in the project.

The DbContext is not in the website project (that I want to publish), but lies in its own dll project that the website project references.

I have tried to publish from both VS 2017 Community and Preview versions on Windows 10. Previously on Windows 8, I could use the Preview version and it worked fine. Now I am on Windows 10.

Som info on my environment:

  • Visual Studio 2017 Community (and Preview)
  • .NET 4.6 with ASP.NET Core and with EF Core
  • Windows 10

Here is the publish dialog: No databases found in the project during publishing

3 Answers

In my case the problem was having the connection string defined in the appsettings.dev.json file but not in the appsettings.json file.

The database was correctly detected in the publish dialog after I moved the connection string into the appsettings.json file.

The solution was to install Visual Studio 2017 Professional and I can now see my DbContext class and apply migrations on publish.

I think that you just need a key for your connection string in the appsettings.json, because probably the publish tool search in appsettings.json for any connection string, and after that, it is looking for dbContect for connection strings which it found.

So, if you have a code like this-

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    var connection = Configuration.GetConnectionString("MyConnectionString");
    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connection));
}

You need to add this-

"ConnectionStrings": {
    "MyConnectionString": ""
}

And the publish tool will looking for MyConnectionString, and will find our registration of DbContext as service with MyConnectionString.

Related