Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
waelalshabani opened this issue Nov 1, 2019 · 3 comments · Fixed by #30070
Closed

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

waelalshabani opened this issue Nov 1, 2019 · 3 comments · Fixed by #30070
Labels
area-tools closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-5.0 punted-for-6.0 punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Milestone

Comments

@waelalshabani
Copy link

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

@ajcvickers
Copy link
Member

@waelalshabani update-database should create the database if it doesn't exist. I will confirm with the team, but this looks to me like a bug with the Pomelo provider.

@olegkap
Copy link

olegkap commented Nov 4, 2019

I have similar issue.
I also get "No DbContext named 'WorkflowDbContext' was found.".
In this case also, all database migration script are in seperate assembly and different DbContext.

When I run "dotnet ef database update", it creates and applies all database migrations from mine and other assembly just fine but if I try to generate script, it only includes changes from my assembly and when I try explicitly specify DbContext and says it cannot be found, am I missing something?

This is link to assembly I am trying to use that have all database migration scripts init.

Thanks,
Oleg

@ajcvickers
Copy link
Member

Note from triage: from the non-provider side, we should surface the error more clearly in cases like this.

@ajcvickers ajcvickers added this to the 5.0.0 milestone Nov 4, 2019
@ajcvickers ajcvickers modified the milestones: 5.0.0, Backlog Jun 9, 2020
@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Nov 2, 2020
@ajcvickers ajcvickers modified the milestones: 6.0.0, Backlog Sep 24, 2021
@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Nov 10, 2021
@ajcvickers ajcvickers added propose-punt punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. and removed propose-punt labels Jul 6, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, Backlog Jul 7, 2022
ajcvickers added a commit that referenced this issue Jan 15, 2023
@ajcvickers ajcvickers changed the title 'No DbContext was found' error. Provide more information when 'No DbContext was found' error is generated Jan 15, 2023
@ajcvickers ajcvickers modified the milestones: Backlog, 8.0.0 Jan 15, 2023
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 15, 2023
@ajcvickers ajcvickers assigned ajcvickers and unassigned bricelam Jan 15, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0, 8.0.0-preview1 Jan 29, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0-preview1, 8.0.0 Nov 14, 2023
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-tools closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-5.0 punted-for-6.0 punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Projects
None yet
5 participants