JoeMcAlly
JoeMcAlly

Reputation: 37

I need to call UseNetTopologySuite in OnConfiguring but not have the connection string added by EF scaffold

Running:

dotnet ef dbcontext scaffold "ConnectionString"
--context "MyContext" Microsoft.EntityFrameworkCore.SqlServer

This creates a context class that has an OnConfiguring method that looks like this:

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 https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlServer("XXXXX", x => x.UseNetTopologySuite());

That connection string gets used instead of the one in the config.

I need an OnConfiguring that looks like this:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(x => x.UseNetTopologySuite());

So I added --no-onconfiguring - perfect no connection string in the context class.

Then I added a partial class:

public partial class MyContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer(x => x.UseNetTopologySuite());
}

The context is great. However OnConfiguring is either called too late or not called at all.

How do I get OnConfiguring to have UseNetTopologySuite() and not a connection string?

Or get the partial class work?

Upvotes: 0

Views: 120

Answers (0)

Related Questions