By default, the current Blazor template uses SQL Server localdb for storing authentication data. You have to do some change when you want to use sqlite instead. Please note that the belows step applies only to new project that just generated by template, where there is no db migration done yet.
Steps Outline:
- Update Nuget package
- Modify Startup.cs and connection string
- Modify Database Migration code
Step 1:
Add Nuget “Microsoft.EntityFrameworkCore.Sqlite” and remove “Microsoft.EntityFrameworkCore.SqlServer”
Step 2:
In Startup.cs, change “UseSqlServer” to “UseSqlite”
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity()
.AddEntityFrameworkStores();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingAuthenticationStateProvider>();
services.AddSingleton();
}
Then modify the connection string in appsetting.json to something like “DataSource=<yourdbname>.db” which point to your sqlite database file.
After Step 2, you may try to build the project. You will get errors in a file “00000000000000_CreateIdentitySchema.cs”, this is in fact the initial database migration action for authentication. We should proceed to step 3 to modify source code.
Step 3:
In file “00000000000000_CreateIdentitySchema.cs“, Change all the occurrance of
“.HasAnnotation(“SqlServer:ValueGenerationStrategy”, SqlServerValueGenerationStrategy.IdentityColumn)” or “.Annotation(“SqlServer:ValueGenerationStrategy”, SqlServerValueGenerationStrategy.IdentityColumn)”
to “.HasAnnotation(“Sqlite:Autoincrement”, true)” or “.Annotation(“Sqlite:Autoincrement”, true)” respectively.
After all above steps, you should be able to build the project without error. Then start the app, try to register a new user. It will prompt you to apply first database migration.