C
C#2y ago
Last_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 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?
5 Replies
Last_Aeon
Last_AeonOP2y ago
Assuming that db is a fresh new instance of the Online_WriterContext, how does db.SaveChangesAsync() know to do the correct thing Here is my Online_WriterContext class, but I don't see how the configuration is inbuilt into the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Online_Writer.Models;

namespace Online_Writer.Data
{
public class Online_WriterContext : DbContext
{
public Online_WriterContext (DbContextOptions<Online_WriterContext> options)
: base(options)
{
}

public DbSet<Online_Writer.Models.Character> Character { get; set; } = default!;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Online_Writer.Models;

namespace Online_Writer.Data
{
public class Online_WriterContext : DbContext
{
public Online_WriterContext (DbContextOptions<Online_WriterContext> options)
: base(options)
{
}

public DbSet<Online_Writer.Models.Character> Character { get; set; } = default!;
}
}
JakenVeina
JakenVeina2y ago
cracks knuckles
I don't see how the configuration is inbuilt into the class
it's not, it's passed in, via options
How does the program know the correct configuration db is supposed to be if db is a fresh new instance of Online_WriterContext
it's passed in via options
How do these two code segments connect to each other?
.AddDbContext<Online_WriterContext>() registers the Online_WriterContext class with your application's IoC container as well as all of its dependencies (which in this case just consists of DbContextOptions<Online_WriteContext>) or probably more specifically it registers Online_WriterContext with custom construction logic that uses a DbContextFactory to construct instances and all of the dependencies of DbContextFactory which includes classes that store configuration/options information configuration/options information that is built within your optionsBuilder delegate that you passed to AddDbContext() so, when you register endpoints with the application, and define Online_WriterContext as a dependency for that endpoint it's the IoC container that resolves that dependency because .AddDbContext() told it how to do so based on the configuration you provided to .AddDbContext()
Last_Aeon
Last_AeonOP2y ago
What is IoC short for? The rest of the explanation is great and I think I understand the gist of it. Thank you!
JakenVeina
JakenVeina2y ago
Inversion of Control whereby you don't create and manage object lifetimes yourself you "invert" that control, and hand it over to someone else the IoC Container
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise 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