ASP.NET Identity 2.0 provider that use Entity Framework for PostgreSQL.
- Install the Npgsql.EntityFramework nuget package
- Update the web.config
<entityFramework>
<defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
- Update the ConnectionString
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=localhost;Database=[your_db_name];User Id=[your_user];Password=[your_password];SearchPath=public;" providerName="Npgsql" />
</connectionStrings>
You do not have to use "DefaultConnection" as the name. If you use a different name, just specify the name in the nameOrConnectionString parameter in the DbConext constructor.
Specify SearchPath in the connection string to specify the schema for the Identity tables. By default, it will use public.
Once you have correctly setup the PostgreSQL EntityFramework, using PostgreSQL.AspNet.Identity.Framework is straight forward.
- Add the PostgreSQL.AspNet.Identity.EntityFramework project as a reference
- Remove the Microsoft.AspNet.Identity.EntityFramework nuget package and replace the "using Microsoft.AspNet.Identity.EntityFramework" statements with "using PostgreSQL.AspNet.Identity.EntityFramework" (it's in IdentityModel.cs and IdentityConfig.cs in the default mvc5 project template)
The default database initalizer is CreateDatabaseIfNotExists. It will automatically create the database if one does not exist and it will also enable database migration. To turnoff off migration
- Run the PostgreSQLIdentity.sql script to create the identity tables in your postgreSQL database. We have to do this since the identity tables will not be created automatically.
- Set the database initalizer to NullDatabaseInitializer to turn off migration.
// put this in the project startup
Database.SetInitializer<ApplicationDbContext>(new NullDatabaseInitializer<ApplicationDbContext>());
An ASP.NET MVC5 template project is included to show it in action. To run the demo project, update the web.config DefaultConnection connection string.
Note. Specify a database name that does not exist to have it create the database automatically for you. Alterntively if you wish to turn off migration, follow step 3 above.
- This is a port of the ASPNET Identity for SQL Server.
- This project got a jump start from AspNet.Identity.PostgreSQL