Create Identity (Membership) database with a different db-schema

803 Views Asked by At

In my asp net core 3.1-project, Im trying out MS Identity, by adding Authentication when I create a new template (Asp net core 3.1+react)

I was hoping to add my Identity tables to a different schema, using this simple Guide, ie

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.HasDefaultSchema("notdbo");
}

But when I run "Update database" in package manager, it still creates a database in dbo-schema

2

There are 2 best solutions below

0
On BEST ANSWER

You need to add a valid migration first, before you do the Update-Database. The catch is that there is already one migration pending in the code, so that's why your change will not be detected.

0
On

Here is some reasons can cause this:

  • The user on on connection string should have access to the schema
  • If you have pending changes on model, this can cause issue. As initial migrations was based on the dbo schema and now you are trying to change the schema when having pending changes. Try to:

Option1:

  • Add-Migration (as mentioned in the comment) - This creates a new migration class as per specified name with the Up() and Down() methods.
  • Update-Database

Option2:

  • Remove the pending changes to the model
  • Run update-database. This will Executes the last migration file created by the Add-Migration command and applies changes to the database schema.
  • Change the schema, and you should be able to recreating the database