Configuring ASP.NET identity with Sqlite

Posted by Anuraj on Wednesday, November 4, 2015

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



Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



MVP
Subscribe


Copyright © 2017 - Anuraj P. 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. Hosted with ❤ by GitHub