Unable to configure AspNet.Identity using PostgreSQL

Viewed 6007

I have a web project that was setup using SQL Server, and that now has to be migrated to PostgreSQL. I'm using Entity Framework version 6.0 with the latest version of Microsoft.AspNet.Identity to manage user credentials. I'm using VS2015 and .NET Framework 452.

The project works fine with the PostgreSQL server for everything except AspNet.Identity. When I try to register a new user or do a login I get the same error message described in this question. Same error, same line but the question is 2 years old and the solution given doesn't work for me (I have tried Add-Migration and Update-Database multiple times).

Everything else works, I have checked and my postgreSQL database does NOT contain any tables related to AspNet.Identity even though they were created automatically when the project was using SQL Server. Other model related tables are present and working.

Thank you.

2 Answers

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

Related