-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
@waelalshabani |
I have similar issue. 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, |
Note from triage: from the non-provider side, we should surface the error more clearly in cases like this. |
…esign-time DbContexts Fixes #18715
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:
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 calledIGenericControlPanel.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: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
The text was updated successfully, but these errors were encountered: