C
C#2y ago
Thinker

❔ ✅ AddSingleton and AddDbConnection are not running whatsoever

I have this code
services.AddSingleton<DbConnection>(_ =>
{
SqliteConnectionStringBuilder stringBuilder = new()
{
DataSource = "MemoryDb",
Mode = SqliteOpenMode.Memory
};
SqliteConnection connection = new(stringBuilder.ToString());
connection.Open();

return connection;
});

services.AddDbContext<MerpieContext>((serviceProvider, options) =>
{
var connection = serviceProvider.GetRequiredService<DbConnection>();
options.UseSqlite(connection, sqliteOptions =>
{
sqliteOptions.UseNodaTime();
});
});
services.AddSingleton<DbConnection>(_ =>
{
SqliteConnectionStringBuilder stringBuilder = new()
{
DataSource = "MemoryDb",
Mode = SqliteOpenMode.Memory
};
SqliteConnection connection = new(stringBuilder.ToString());
connection.Open();

return connection;
});

services.AddDbContext<MerpieContext>((serviceProvider, options) =>
{
var connection = serviceProvider.GetRequiredService<DbConnection>();
options.UseSqlite(connection, sqliteOptions =>
{
sqliteOptions.UseNodaTime();
});
});
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
Tvde1
Tvde12y ago
Is the line services.AddSingleton<DbConnection>(_ => hit?
Thinker
ThinkerOP2y ago
Yes.
Tvde1
Tvde12y ago
are you really calling this method? alright what kind of an instance do you get if DI provides you with a MerpieContext?
Thinker
ThinkerOP2y ago
wdym?
Tvde1
Tvde12y ago
do you ever request a MerpieContext in a controller or such
Thinker
ThinkerOP2y ago
Yes
private void RunMigrations()
{
using var scope = Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<MerpieContext>();

context.Database.Migrate();
}
private void RunMigrations()
{
using var scope = Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<MerpieContext>();

context.Database.Migrate();
}
Tvde1
Tvde12y ago
and this runs fine?
Thinker
ThinkerOP2y ago
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.
Tvde1
Tvde12y ago
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 twice
Thinker
ThinkerOP2y ago
Before that I'm actually removing all MerpieContext because this is supposed to replace any previous configuration.
Tvde1
Tvde12y ago
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"
Thinker
ThinkerOP2y ago
Well other breakpoints are being hit at least
Mayor McCheese
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
Thinker
ThinkerOP2y ago
at this point a file would probably be like lightyears easier lol
Mayor McCheese
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
Thinker
ThinkerOP2y ago
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
phaseshift
phaseshift2y ago
Suggest: Put only that service registration bit in a service collection in a unit test and verify the behaviour with a minimal example
Thinker
ThinkerOP2y ago
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.
djmurp
djmurp2y ago
could you put a breakpoint in the constructor of your context, then look at stacktrace to see where it's being created?
Thinker
ThinkerOP2y ago
can try that
djmurp
djmurp2y ago
if it's not your lambda above then it must be somewhere else
Thinker
ThinkerOP2y ago
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 is
djmurp
djmurp2y ago
nice 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?
Thinker
ThinkerOP2y ago
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 catsleep
phaseshift
phaseshift2y ago
Mark closed then rulesowo
Thinker
ThinkerOP2y ago
yes
Tvde1
Tvde12y ago
nice
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server