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

Stop generating a default DbContext constructor when using dotnet ef dbcontext scaffold --no-onconfiguring #23515

Closed
0xced opened this issue Nov 27, 2020 · 4 comments · Fixed by #26470
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-enhancement
Milestone

Comments

@0xced
Copy link
Contributor

0xced commented Nov 27, 2020

Version 5.0.0 of the dotnet-ef tool introduced the --no-onconfiguring option which is great if you plan to configure your DbContext outside of the context itself (which you probably should).

When using this option, the DbContext class is still generated with a default constructor. This default constructor becomes problematic without the OnConfiguring method since the context is unusable when created through the default constructor. As soon as you try to use a DbContext created by the default constructor, you get a System.InvalidOperationException:

No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

The scaffolded DbContext classes should be fool proof. So I strongly encourage you to consider not generating the default constructor when the --no-onconfiguring option is enabled.

@ajcvickers
Copy link
Member

The default constructor can still be used if OnConfiguring is in a partial class. However, if there is already a partial class for OnConfiguring, then adding a parameterless constructor to that partial seems reasonable, so we will consider this as a future enhancement.

@ajcvickers ajcvickers added this to the Backlog milestone Nov 30, 2020
0xced added a commit to 0xced/efcore that referenced this issue Oct 27, 2021
Version 5.0.0 of the dotnet-ef tool introduced the `--no-onconfiguring` option which is great if you plan to configure your DbContext outside of the context itself (which you probably should).

When using this option, the DbContext class is still generated with a default constructor. This default constructor becomes problematic without the `OnConfiguring` method since the context is unusable when created through the default constructor. As soon as you try to use a DbContext created by the default constructor, you get a `System.InvalidOperationException`:
> No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

Fixes dotnet#23515
0xced added a commit to 0xced/efcore that referenced this issue Oct 27, 2021
Version 5.0.0 of the dotnet-ef tool introduced the `--no-onconfiguring` option which is great if you plan to configure your DbContext outside of the context itself (which you probably should).

When using this option, the DbContext class is still generated with a default constructor. This default constructor becomes problematic without the `OnConfiguring` method since the context is unusable when created through the default constructor. As soon as you try to use a DbContext created by the default constructor, you get a `System.InvalidOperationException`:
> No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

Fixes dotnet#23515
@dsmistry
Copy link

Thank you @0xced. Looking forward to using this as soon as available since I am attempting to use DbContextFactory which fails because of two constructors.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 27, 2021

EF Core Power Tools solves this for you today.

@ajcvickers
Copy link
Member

@dsmistry DbContextFactory ignores the parameterless constructor in EF Core 6.0. See https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/whatsnew#dbcontextfactory-ignores-dbcontext-parameterless-constructor

@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Nov 2, 2021
@ajcvickers ajcvickers added community-contribution closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Nov 2, 2021
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview1 Feb 14, 2022
@ajcvickers ajcvickers changed the title Proposal: prevent generating a default DbContext constructor when using dotnet ef dbcontext scaffold --no-onconfiguring Stop generating a default DbContext constructor when using dotnet ef dbcontext scaffold --no-onconfiguring Feb 28, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-preview1, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants