C#C
C#3y ago
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.")));


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);
});


Which has Online_WriterContext db as the input. How does the program know the correct configuration
db
is supposed to be if
db
is a fresh new instance of Online_WriterContext. How do these two code segments connect to each other?
image.png
Was this page helpful?