dotnet / dotnet/EntityFramework.Docs

How does design time DbContext creation work when using top level statements with WebApplication.CreateBuilder?

Open
#5,052 2 comments 0 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

### Type of issue

Missing information

### Description

The documentation discusses how it tries to integrate with the older pattern of the HostBuilder:

> The tools first try to obtain the service provider by invoking Program.CreateHostBuilder(), calling Build(), then accessing the Services property.

eg. If your code contained a Program.cs file containing:
```
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup();
```

But what about when using the more recent pattern of top level statements, minimal APIs, and the `WebApplication.CreateBuilder(args)`. ie:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/webapplication?view=aspnetcore-9.0
```
var builder = WebApplication.CreateBuilder(args);
builder.Services.Add...
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
```

In this case, there's no obvious `CreateHostBuilder` function, does EF completely skip this step and try resolving via the default DbContext constructor? It's not immediately obvious the way the docs are written, and given this is what most devs will experience with a newly scaffolded dotnet project, the documentation should probably cover this case as the first scenario.

### Page URL

https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli

### Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/cli/dbcontext-creation.md

### Document Version Independent Id

d988e806-cb86-19f5-7f76-6ce3bfcd94ed

### Platform Id

814da52c-6a57-17fb-d83c-680ade4b45c0

### Article author

@SamMonoRT

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.