Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design-time DbContext Creation does not bypass the DI #4782

Open
andrey-kondratov opened this issue Aug 19, 2024 · 0 comments
Open

Design-time DbContext Creation does not bypass the DI #4782

andrey-kondratov opened this issue Aug 19, 2024 · 0 comments

Comments

@andrey-kondratov
Copy link

Type of issue

Other (describe below)

Description

Section "From a design-time factory" states:

If a class implementing this interface is found in either the same project as the derived DbContext or in the application's startup project, the tools bypass the other ways of creating the DbContext and use the design-time factory instead.

However, I see in my dotnet ef migrations add InitialCreate logs that there is a warning first from trying the DI approach, and only then a successful message after using the design-time factory:

dotnet ef migrations add InitialCreate --context BloggingDbContext
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: (One of my errors during DI startup because of no config given).
Done. To undo this action, use 'ef migrations remove'

This answer on StackOverflow also notes that:

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

My setup was:

    <PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />

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

Article author

@bricelam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant