Starnuto di topo
Starnuto di topo

Reputation: 3569

Entity Framework 7 Scaffold-DbContext overrides OnConfiguring without checking if (!optionsBuilder.IsConfigured)

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:

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?

Upvotes: 0

Views: 821

Answers (1)

ErikEJ
ErikEJ

Reputation: 41799

You can use the -NoOnConfiguring option

https://learn.microsoft.com/en-us/ef/core/cli/powershell#scaffold-dbcontext

Upvotes: 2

Related Questions