Skip to content

Commit b5d2712

Browse files
committed
work
1 parent 460d084 commit b5d2712

File tree

88 files changed

+83882
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+83882
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ A typical unit-of-work when using Entity Framework Core (EF Core) involves:
2929

3030
> [!IMPORTANT]
3131
>
32-
> - It is very important to dispose the <xref:Microsoft.EntityFrameworkCore.DbContext> after use. This ensures both that any unmanaged resources are freed, and that any events or other hooks are unregistered so as to prevent memory leaks in case the instance remains referenced.
32+
> - It is important to dispose the <xref:Microsoft.EntityFrameworkCore.DbContext> after use. This ensures both that any unmanaged resources are freed, and that any events or other hooks are unregistered so as to prevent memory leaks in case the instance remains referenced.
3333
> - [DbContext is **not thread-safe**](#avoiding-dbcontext-threading-issues). Do not share contexts between threads. Make sure to [await](/dotnet/csharp/language-reference/operators/await) all async calls before continuing to use the context instance.
3434
> - An <xref:System.InvalidOperationException> thrown by EF Core code can put the context into an unrecoverable state. Such exceptions indicate a program error and are not designed to be recovered from.
3535
@@ -48,8 +48,11 @@ ASP.NET Core applications are [configured using dependency injection](/aspnet/co
4848
options => options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));
4949
}
5050
-->
51+
5152
[!code-csharp[ConfigureServices](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp/Startup.cs?name=ConfigureServices)]
5253

54+
[!code-csharp[ConfigureServices](~/Miscellaneous/ConfiguringDbContext/WebApp/Program.cs?name=snippet_1)]
55+
5356
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.
5457

5558
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:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "/Pages/Shared/_Layout.cshtml";
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace WebApp9.Data
5+
{
6+
public class ApplicationDbContext : IdentityDbContext
7+
{
8+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
9+
: base(options)
10+
{
11+
}
12+
}
13+
}

samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs

Lines changed: 277 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)