doc/WebSite/EF-Core-Sqlite-Integration.md
Download a starter template with ASP.NET Core and Entity Framework Core to integrate SQLite. Multi-page template with ASP.NET Core 2.x + .NET Core Framework + Authentication will be explained in this document.
Install the Microsoft.EntityFrameworkCore.Sqlite NuGet package to the *.EntityFrameworkCore project.
Some configuration and workarounds are needed to use SQLite with ASP.NET Core and Entity Framework Core.
Since SQLite doesn't support multithreading, transactions should be disabled in the SQLiteDemoEntityFrameworkModule.PreInitialize() method.
[DependsOn(
typeof(SQLiteDemoCoreModule),
typeof(AbpZeroCoreEntityFrameworkCoreModule))]
public class SQLiteDemoEntityFrameworkModule : AbpModule
{
public override void PreInitialize()
{
...
// add this line to disable transactions
Configuration.UnitOfWork.IsTransactional = false;
...
}
}
Replace YourProjectNameDbContextConfigurer.cs with the following lines
public static class SqliteDemoDbContextConfigurer
{
public static void Configure(DbContextOptionsBuilder<SqliteDemoDbContext> builder, string connectionString)
{
builder.UseSqlite(connectionString);
}
public static void Configure(DbContextOptionsBuilder<SqliteDemoDbContext> builder, DbConnection connection)
{
builder.UseSqlite(connection);
}
}
Change the connection string to your SQLite connection in *.Web.Mvc/appsettings.json. Example:
{
"ConnectionStrings": {
"Default": "Data Source=SqliteDemoDb.db;Cache=Shared"
},
...
}
Remove all migration classes(include DbContextModelSnapshot) under *.EntityFrameworkCore/Migrations folder before creating database.
Microsoft.EntityFrameworkCore.Sqlite will add some of its own configuration to work with Entity Framework Core.
Now it's ready to build the database.
add-migration Initial_Migration commandupdate-database commandThe SQLite integration is now complete. You can now run your project with SQLite.