You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example registers a `DbContext`subclass called `ApplicationDbContext` as a scoped service in the ASP.NET Core application service provider (a.k.a. the dependency injection container). The context is configured to use the SQL Server database provider and will read the connection string from ASP.NET Core configuration. It typically does not matter _where_ in `ConfigureServices` the call to `AddDbContext` is made.
56
+
The preceding code registers `ApplicationDbContext`, a subclass of `DbContext` as a scoped service in the ASP.NET Core app service provider. The service provider is also know as the dependency injection container. The context is configured to use the SQL Server database provider and reads the connection string from ASP.NET Core configuration.
57
57
58
58
The `ApplicationDbContext` class must expose a public constructor with a `DbContextOptions<ApplicationDbContext>` parameter. This is how context configuration from `AddDbContext` is passed to the `DbContext`. For example:
Copy file name to clipboardExpand all lines: samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Program.cs
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,15 @@
3
3
usingWebApp9.Data;
4
4
5
5
varbuilder=WebApplication.CreateBuilder(args);
6
-
7
-
// Add services to the container.
8
-
varconnectionString=builder.Configuration.GetConnectionString("DefaultConnection")??thrownewInvalidOperationException("Connection string 'DefaultConnection' not found.");
0 commit comments