❔ ✅ AddSingleton and AddDbConnection are not running whatsoever
I have this code
The configuration lambdas are not running. I have no idea why. The
DbConnection
and MerpieContext
are being added to the services, but they're not being configured.
I've tried setting breakpoints inside the lambdas but they're never hit.28 Replies
Is the line
services.AddSingleton<DbConnection>(_ =>
hit?Yes.
are you really calling this method?
alright
what kind of an instance do you get if DI provides you with a
MerpieContext
?wdym?
do you ever request a
MerpieContext
in a controller or suchYes
and this runs fine?
Well I get an error on
context.Database.Migrate();
that EF can't find the DB file, which I assume is because the connection string is never set.
But the first two lines run fine.I suppose you're not accidentally adding
MerpieContext
to the services somewhere else
e.g. a previous configuration
I'm not sure what happens if a context is added twiceBefore that I'm actually removing all
MerpieContext
because this is supposed to replace any previous configuration.as a sanity check, what happens if you run it with
services.AddDbContext<MerpieContext>((serviceProvider, options)
commented out?
hmm
And also check whether break points are on. E.g. you're running debug in rider and not just "Run"Well other breakpoints are being hit at least
Hmm I think I ran into this issue a couple years back and had run an ensure db created method
Ultimately I switched over to a file but I forget why
at this point a file would probably be like lightyears easier lol
Oh yeah I remember now why https://stackoverflow.com/questions/56319638/entityframeworkcore-sqlite-in-memory-db-tables-are-not-created
I generally don’t bother with SQLite tbh unless it’s really throw away. I know I’m a loner there
This is exactly what I'm trying to do, as seen above
I'm trying to create a connection, but the configuration is not being run
Moving the connection stuff outside the configuration lambda does nothing
Still getting that exception
The file doesn't exist
It can't find it
But it shouldn't even be looking for a file
So the DB is still not being configured
BUT WHY
Like is this about EF or about MSDI?
Is the DI stuff just not working??
Is this some arcane bug that literally no one has reported
Suggest: Put only that service registration bit in a service collection in a unit test and verify the behaviour with a minimal example
Okay. So I put all that into a separate test, ran it... and it works.
The breakpoints are being hit, the DB context is being resolved, the
Migrate()
call works.
So... that's weird.
Then perhaps the issue is that some service descriptors already exist in the service collection which causes it to not create it through the configuration.could you put a breakpoint in the constructor of your context, then look at stacktrace to see where it's being created?
can try that
if it's not your lambda above then it must be somewhere else
Okay so the constructor is being hit twice, which is expected. Once from Program.cs and once from my
WebApplicationFactory
.
So I again suspect that there's some service I haven't properly cleared out which causes it to not run the configuration.
okay I think I have an idea what the problem isnice
Is it because a servicedescriptor<MerpieContext> is added (the factory), but you were clearing out all MerpieContext even though there's none, so it's using the descriptor from Program.cs instead?
Okay, the issue was that I was clearing
MerpieContext
but not DbContextOptions
or DbContextOptions<MerpieContext>
, so it didn't re-create those.
works fine now Mark closed then
yes
nice
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.