Skip to content

Commit a9e8264

Browse files
committed
work
1 parent c3eeafd commit a9e8264

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ 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[snippet_1](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Program.cs?name=ConfigureSersnippet_1vices)]
55-
56-
C:\GH\EntityFramework.Docs\samples\core\Miscellaneous\ConfiguringDbContext\WebApp9\Program.cs
57-
[!code-csharp[ConfigureServices](~/core/Miscellaneous/ConfiguringDbContext/WebApp9/Program.cs?name=snippet_1)]
54+
[!code-csharp[snippet_1](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Program.cs?name=snippet_1)]
5855

5956
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.
6057

@@ -69,9 +66,12 @@ The `ApplicationDbContext` class must expose a public constructor with a `DbCont
6966
}
7067
}
7168
-->
69+
7270
[!code-csharp[ApplicationDbContext](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp/ApplicationDbContext.cs?name=ApplicationDbContext)]
7371

74-
`ApplicationDbContext` can then be used in ASP.NET Core controllers or other services through constructor injection. For example:
72+
[!code-csharp[dbContext](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Data\ApplicationDbContext.cs)]
73+
74+
`ApplicationDbContext` can be used in ASP.NET Core controllers or other services through constructor injection:
7575

7676
<!--
7777
public class MyController
@@ -86,6 +86,7 @@ The `ApplicationDbContext` class must expose a public constructor with a `DbCont
8686
-->
8787
[!code-csharp[MyController](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp/Controllers/MyController.cs?name=MyController)]
8888

89+
8990
The final result is an `ApplicationDbContext` instance created for each request and passed to the controller to perform a unit-of-work before being disposed when the request ends.
9091

9192
Read further in this article to learn more about configuration options. In addition, see [App startup in ASP.NET Core](/aspnet/core/fundamentals/startup) and [Dependency injection in ASP.NET Core](/aspnet/core/fundamentals/dependency-injection) for more information on configuration and dependency injection in ASP.NET Core.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page
2+
@model MyModel
3+
@{
4+
ViewData["Title"] = "Privacy Policy";
5+
}
6+
<h1>@ViewData["Title"]</h1>
7+
8+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.Mvc.RazorPages;
2+
3+
4+
public class MyModel : PageModel
5+
{
6+
private readonly ApplicationDbContext _context;
7+
8+
public MyModel(ApplicationDbContext context)
9+
{
10+
_context = context;
11+
}
12+
13+
public void OnGet()
14+
{
15+
}
16+
}
17+
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.AspNetCore.Mvc.RazorPages;
33

4-
namespace WebApp9.Pages
5-
{
4+
65
public class PrivacyModel : PageModel
76
{
8-
private readonly ILogger<PrivacyModel> _logger;
7+
private readonly ApplicationDbContext _context;
98

10-
public PrivacyModel(ILogger<PrivacyModel> logger)
11-
{
12-
_logger = logger;
13-
}
149

15-
public void OnGet()
10+
public PrivacyModel(ApplicationDbContext context)
11+
{
12+
_context = context;
13+
}
14+
15+
public void OnGet()
1616
{
1717
}
1818
}
1919

20-
}

0 commit comments

Comments
 (0)