C
C#14mo 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_Aeon14mo 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
JakenVeina14mo 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_Aeon14mo 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
JakenVeina14mo 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
Accord14mo 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
More Posts
❔ Difficulties with http requests for image(blob) column in mysqlSo i recently learned how to pull basic data from mysql but am struggling to find anything I think iCannot access file because it's being used by another processI am getting the following error when I try to run my program: `System.IO.IOException: 'The process print out all pairs of natural numbers whose sum isequal to the entered number nWrite an algorithm that prints out all pairs of natural numbers whose sum is equal to the entered nu❔ "BLAZOR102: The scoped css file '~\Layout.cshtml.css' was defined but..." How do I fix this error?I've been messing around with ASP.NET and trying to make a new web app, originally I was having issu❔ Unity 3D Urp ProblemsDoes anyone know why when i make a 3d urp project, nothing shows up on my camera? The camera is look✅ im new and i dont get thisi get error cs 1525 unexpected symbol on code line 8 and 16✅ `DllImport` on a dll name only known at runtimeI have a bunch of native dlls that have the same interface (they export the same functions with iden❔ Some theory questions I'm unsure about :)I just have a few questions that I'm unsure about when I was revising for an exam. Sorry if some are✅ [SOLVED] Need help with converting async method to a 'normal' methodI tried a few solutions to be able to call an async method from my Main method but those have failed✅ Help me pleaseI am trying to do a project using asp.net identity. I am following n-layer mvc architecture, keeping