Unable to configure AspNet.Identity using PostgreSQL Unable to configure AspNet.Identity using PostgreSQL postgresql postgresql

Unable to configure AspNet.Identity using PostgreSQL


I was unable to make Microsoft's AspNet.Identity to work with postgresql, it seems to be designed to work with SQL Server and doesn't acknowledge the presence of a different data context configuration no matter what you do.

I have everything working now with the aid of an external library so I will provide an answer to my question in case someone else runs into this problem.

I followed the instructions present in the following project (credit to vincechan):

Step 2 was a bit problematic. After importing the project into my solution I had to resolve reference issues in it. In the Package Manager Console you can Install the missing packages and/or update the ones that are outdated. My final configuration required using:

  • Npgsql 3.2.2 on my main project (using EntityFramework6.Npgsql)
  • Npgsql 2.2.7 on the downloaded project (using Npgsql.EntityFramework)

Once you have everything ready, add a new migration (Add-Migration <migration-name) and update your database (Update-Database).

Now execute the SQL script that creates Identity related tables, they will not be created automatically like the do with SQL Server. The script is included in the project (file PostgreSQLIdentity.sql).

Everything should work now. Forgive the poor formatting of this answer.


If your PostgreSQL database doesn't contain any tables related to ASP.NET Identity, then it most likely means that migrations are not enabled for ASP.NET Identity context class. In my case, I'm using VS scaffolding and I mean this class:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

So, the first step is to run Enable-Migrations in Package Manager Console without parameters. If you have a clean database then it will work exactly how it worked in the SO topic mentioned earlier. So if you want a simple solution then destroy your PostgreSQL database completely, create it again and simply run Enable-Migrations.

If you don't want to destroy your PostgreSQL database or/and lose any migrations done before then just enable migrations only for ASP.NET Identity context in a separate directory. There already was a SO answer explaining how to do this.

Enable migrations in some directory (e.g. MigrationsIdentity) for ApplicationDbContext:

Enable-Migrations -ContextTypeName MyProject.Models.ApplicationDbContext -MigrationsDirectory MigrationsIdentity

Add initial migration for this context:

Add-Migration IdentityInitial -ConfigurationTypeName MyProject.MigrationsIdentity.Configuration

Apply this migration:

Update-Database -ConfigurationTypeName MyProject.MigrationsIdentity.Configuration

After these steps there will be ASP.NET Identity tables in your PostgreSQL database generated automatically by Code First, you don't need to run anything manually with SQL-scripts.

In the comment you've mentioned that you have separate contexts. However there is a disadvantage of multiple contexts approach: you won't use them together easily. Also you will have to specify explicitly the context with -ConfigurationTypeName flag every time you migrate your database. I would use single context as was discussed already, but it depends on the requirements of your task.

Versions:

  • EntityFramework 6

  • Microsoft.AspNet.Identity.Core 2.1

  • Npgsql 3.1.10.0

  • EntityFramework6.Npgsql 3.1.1.0