Skip to content

Provide more information when 'No DbContext was found' error is generated #18715

@w-alsh

Description

@w-alsh

I have already solved what I'm facing, but thought I should report it since I struggled for an hour trying to figure out the following error message which I think it's unrelated to the problem I had:

No DbContext was found in assembly 'Sanaya.Website'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

Steps to reproduce

I have a .dll file that contains the migrations classes and DbContext class which were generated by a different solution.

  • I created an ASP.NET Core 2.2 project in a separate solution and added this .dll package as reference.

  • In Startup.cs, I added the database context to the services as follow:
    serviceCollection.AddDbContext<CPDbContext>(options => options.UseMySql(configuration.GetConnectionString(ConfigurationStrings.DefaultConnection), sqlDbOptions => sqlDbOptions.MigrationsAssembly(ConfigurationStrings.MigrationAssembly)));

    where ConfigurationStrings.MigrationAssembly is a const that hold the namespace in the .dll that have the migration and DbContext, which is called IGenericControlPanel.MySql

Then went to Package Manager Console and hit update-database and was given the error I previously mentioned.

Then to get more information I used update-database -verbose and get the following error:

fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'IGenericControlPanel.MySql.Database.CPDbContext'.
MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown database 'sanaya.db'
at MySqlConnector.Core.ServerSession.SwitchAuthenticationAsync(ConnectionSettings cs, PayloadData payload, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 560
at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSessio

I solve it by creating the database first using sql:
CREATE SCHEMA 'sanaya.db' ;
where 'sanaya.db' is the name of my database that I specified in my connection string.
"DefaultConnection": "server=***;database=sanaya.db;user=***;password=***"

Then use update-database and everything worked correctly, the tables were created successfully and everything worked perfectly.

I don't know whether it's a problem from the provider (I used Pomelo.EntityFrameworkCore.MySql as mentioned in 'Further technical details' section below) or from EF Core but I felt that this might be an issue that I need to report because it might help someone who come across the same thing.

Further technical details

EF Core version: 2.2.6
Database provider: Pomelo.EntityFrameworkCore.MySql {2.2.6}
Target framework: .NET Core 2.2
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.6

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions