dotnet / dotnet/AspNetCore.Docs

Documentation about Seeding for Program.cs

Open
#31,361 0 comments 0 reactions 1 assignee Claimed by @wadepickett View on GitHub
Source - Docs.ms
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

### Description

Program.cs does not grab a DbContext from DI. That is done in SeedData.

I would change this:

In the previous code, Program.cs has been modified to do the following:

Get a database context instance from the dependency injection (DI) container.
Call the seedData.Initialize method, passing to it the database context instance.
Dispose the context when the seed method completes. The using statement ensures the context is disposed.

to this:

In the previous code, Program.cs has been modified to do the following:

Create a new IServiceScope.
From the new IServiceScope fetch the IServiceProvider
Call the seedData.Initialize method, passing to it the IServiceProvider so that SeedData.Initialize.
Dispose the IServiceScope when the seed method completes. The using statement ensures the IServiceScope is disposed.

### Page URL

https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/sql?view=aspnetcore-8.0&tabs=visual-studio

### Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/tutorials/razor-pages/sql.md

### Document ID

b8c7d824-35ca-68fb-e3be-839ae886657c

### Article author

@wadepickett

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.