-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
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. |
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
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
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. |
EF Core Power Tools solves this for you today. |
@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 |
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 aSystem.InvalidOperationException
: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.The text was updated successfully, but these errors were encountered: