EF Core and environment variables
Hello! I have a project that is running with .NET 8.0 (but originally generated from 3.1, so not using the new Startup file type). It's a WebApi project with EF Core. I am trying to generate a DbContext script using
dotnet ef dbcontext script
but receiving an error. The error has to do with an environment variable reference in the Startup class that is coming back as null. My environment variables are stored in a file called .env.
When I run the project through VS Code, the launch.json has a reference to the .env file and all is well. But when I run the EF Core CLI, the environment variables aren't getting picked up, even when I source the environment variable file in the terminal with source .env
(this is on macOS).
My question is: how can I get the EF Core CLI to play nice with the environment variables that are stored in the file so I don't get errors?9 Replies
Not sure if this is applicable for you, but if you're just trying to start / run the project for EF tooling, you might want to add something like this to your application's entrypoint:
IIRC this was added in recent EF versions but could be wrong.
I use it in a few projects when generating migrations otherwise they'll cause EF tooling to fail.
Interesting. Let me look into that.
I put that in my Main method in Program.cs and it makes no difference
Ok so maybe there isn't an issue around the environment variables because I'm still getting the other after even after the addition of that boolean. The error is nice and vague
Unable to create a 'DbContext' of type ''
What does your dbcontext look like?
It looks something like this (I took out the DbSets and entity builder stuff because it's probably not relevant here)
https://gist.github.com/hawk4031/b4366ef32fea0265921d6e88e2beae45
I'm adding the dbcontext in the startup file with
services.AddDbContext()
. I'm also making use of the environment variables there to build up the connection string and set it with UseNpgsql(connectionString)
.I think it's failing due to being configured with
DbContextOptions
, so the tooling ends up trying to make a DbContext
with no provider specifiedI'll point out that I did recently upgrade this project from .NET 6 to .NET 8. In .NET 6, there were no problems but I haven't done any migrations or SQL script generations since the change.
Might be some library versioning issues then as well. Take a look at this stack thread, it sounds similar to your errors: https://stackoverflow.com/questions/75701125/unable-to-create-a-dbcontext
Stack Overflow
Unable to create a 'DBContext'
i have the following error message:
Unable to create a 'DbContext' of type ''. The exception 'No database provider has been configured for this DbContext. A provider can be configured by overridin...
I think my library versions are matched up properly according to the Npgsql documentation. I also tried the second answer in that post, but it doesn't make any difference there either.
One thing I'm going to try now is using IDesignTimeDbContextFactory
Ok cool I got it! I create a new class implementing IDesignTimeDbContextFactory that could read the environment variables and it's able to properly create the design-time DbContext and do what I need it.
Thanks for your help @ded
:PepoSalute: