Configuring ASP.NET identity with Sqlite

November 04, 2015 by Anuraj

aspnet5 sqlite autofac

This post is about configuring the ASP.NET identity with Sqlite Database. In ASP.NET5, you can configure the identity to use Sqlite by modifying the DbContext. Instead of using SqlServer, you can use Sqlite.

Here is the Startup.cs, configure services method.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddEntityFramework()
        .AddSqlite()
        .AddDbContext<TodoDbContext>();

    services.AddIdentity<ToDoUser, IdentityRole>()
        .AddEntityFrameworkStores<TodoDbContext>()
        .AddDefaultTokenProviders();
}

And the Db context, you need override the OnConfiguring() method, and you can specify the provider with connection string.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlite(@"Data Source=IdentitySample.sqlite;");
}

And in the project.json file, you need to mention the Sqlite dependency as well.

"dependencies": {
  "Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta8",
  "Microsoft.AspNet.Diagnostics": "1.0.0-beta8",
  "Microsoft.AspNet.Identity": "3.0.0-beta8",
  "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta8",
  "Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8",
  "Microsoft.AspNet.Mvc": "6.0.0-beta8",
  "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
  "EntityFramework.Sqlite": "7.0.0-beta8"
},

Here is the Sqlite Database created.

Sqlite Identity Database

Happy Programming

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub