© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
25 replies
Aeon

❔ Question on how AddDbContext works

I have some beginner question. I'm confused about how
builder.Services.AddDbContext<Online_WriterContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Online_WriterContext") ?? throw new InvalidOperationException("Connection string 'Online_WriterContext' not found.")));
builder.Services.AddDbContext<Online_WriterContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Online_WriterContext") ?? throw new InvalidOperationException("Connection string 'Online_WriterContext' not found.")));


works.

Later in the guide, they have

app.MapPost("/character", async( Character character, Online_WriterContext db) => {
    db.Character.Add(character);
    await db.SaveChangesAsync();
    return Results.Created($"/character/{character.Id}", character);
});
app.MapPost("/character", async( Character character, Online_WriterContext db) => {
    db.Character.Add(character);
    await db.SaveChangesAsync();
    return Results.Created($"/character/{character.Id}", character);
});


Which has
Online_WriterContext db
Online_WriterContext db
as the input. How does the program know the correct configuration
db
db
is supposed to be if
db
db
is a fresh new instance of
Online_WriterContext
Online_WriterContext
. How do these two code segments connect to each other?
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ builder.Services.AddDbContext<>
C#CC# / help
3y ago
Help on how async/await works
C#CC# / help
2y ago
❔ How DI works?
C#CC# / help
4y ago
✅ How does dependencies works ?
C#CC# / help
15mo ago