I switched from EF6 to EF7 and, rebuilding my classes using the Scaffold-DbContext
command, I noticed that the generated DbContext
has a different implementation of the OnConfiguring
method.
EF6:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
optionsBuilder.UseNpgsql("...");
}
}
EF7:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
optionsBuilder.UseNpgsql("...");
}
My new package references are:
- Microsoft.EntityFrameworkCore" Version="7.0.1"
- Microsoft.EntityFrameworkCore.PostgreSQL" Version="7.0.1"
- Microsoft.EntityFrameworkCore.Tools" Version="7.0.1"
This missing check is really painful and I'd like to avoid to manually insert it again whenever I rebuild my classes.
To avoid the problem, I thought to use a class derived from the scaffolded DbContext, implementing the OnConfiguring
method.
Although this works, it seems to me really weird, as I have to override an existing method with one that... does absolutely nothing!
Is there a better way of doing the job? Maybe, a way to tell Scaffold-DbContext
not to scaffold the OnConfiguring
method at all?
CodePudding user response:
You can use the -NoOnConfiguring option.
https://learn.microsoft.com/en-us/ef/core/cli/powershell#scaffold-dbcontext