title: Postgres EF Migrations "3F000: No schema has been selected to create in"
body: <p>I am attempting to execute an initial database creation migration using entity framework core against a postgres database.
  The problem I am having is that I wish to create the tables under a custom schema.
  I have managed to create an initial migration with no problems but when I attempt to &quot;update-database&quot; the migration fails with the following error.</p>
  <pre><code>Npgsql.PostgresException (0x80004005): 3F000: no schema has been selected to create in
  </code></pre>
  <p>at Npgsql.NpgsqlConnector.&lt;&gt;c__DisplayClass160_0.&lt;g__ReadMessageLong|0&gt;d.MoveNext()</p>
  <p>Having initial looked into the issue I assumed this was simply because the schema was not being set in the context.
  To get around this I set added the following code to the DbContext</p>
  <pre><code>    protected override void OnModelCreating(ModelBuilder builder)
      {
          //Set the default schema
          builder.HasDefaultSchema(&quot;ConfigStore&quot;);
          
          //Continue with the call./Migrate
          base.OnModelCreating(builder);
      }
  </code></pre>
  <p>I still get the same error when running update-database.
  I checked the initial migration Up() method and can clearly see the following code:</p>
  <pre><code>migrationBuilder.EnsureSchema(name: &quot;ConfigStore&quot;);
  </code></pre>
  <p>The migration creates the database but nothing else so I assume the problem here is that the schema is not being created after the database which is then subsequently causing the table creations to fail.
  The question I have is how do I fix this?
  Can I execute some custom sql AFTER the database has been created but before the tables? Is there something I can do to get EnsureSchema() to create the schema first?</p>
  <p>Thanks in advance</p>