Skip to content

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

@tysonstolarski

Description

@tysonstolarski

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<Startup>();

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions