Skip to content

Commit 6293363

Browse files
committed
work
1 parent b5d2712 commit 6293363

File tree

2 files changed

+9
-7
lines changed
  • entity-framework/core/dbcontext-configuration
  • samples/core/Miscellaneous/ConfiguringDbContext/WebApp9

2 files changed

+9
-7
lines changed

entity-framework/core/dbcontext-configuration/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ ASP.NET Core applications are [configured using dependency injection](/aspnet/co
5151

5252
[!code-csharp[ConfigureServices](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp/Startup.cs?name=ConfigureServices)]
5353

54-
[!code-csharp[ConfigureServices](~/Miscellaneous/ConfiguringDbContext/WebApp/Program.cs?name=snippet_1)]
54+
[!code-csharp[ConfigureServices](~/Miscellaneous/ConfiguringDbContext/WebApp9/Program.cs?name=snippet_1)]
5555

56-
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.
5757

5858
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:
5959

samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Program.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
using WebApp9.Data;
44

55
var builder = WebApplication.CreateBuilder(args);
6-
7-
// Add services to the container.
8-
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
6+
// <snippet_1>
7+
var connectionString =
8+
builder.Configuration.GetConnectionString("DefaultConnection")
9+
?? throw new InvalidOperationException("Connection string"
10+
+ "'DefaultConnection' not found.");
911
builder.Services.AddDbContext<ApplicationDbContext>(options =>
1012
options.UseSqlServer(connectionString));
13+
// </snippet_1>
14+
1115
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
1216

1317
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
@@ -16,15 +20,13 @@
1620

1721
var app = builder.Build();
1822

19-
// Configure the HTTP request pipeline.
2023
if (app.Environment.IsDevelopment())
2124
{
2225
app.UseMigrationsEndPoint();
2326
}
2427
else
2528
{
2629
app.UseExceptionHandler("/Error");
27-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
2830
app.UseHsts();
2931
}
3032

0 commit comments

Comments
 (0)