I use MAUI (.NET 8) for my Android application. I've added SQLite database file, previously configured from SQLite Studio. Tested connection on desktop (Windows 11 on my local machine) - it works well. But after switching Debug target to Android, I can run app, but can't use my database. I tried to attach database as Embedded Resource, but still no positive effect to my issue. Also tried to use relative path: "/files/psyonicru_localdb.sqlite". Database configuration in code listed below:
public static IServiceCollection WithDatabase(this IServiceCollection builderServices, IConfiguration builderConfiguration)
{
var databaseConfig = builderConfiguration.GetSection("Database").Get<DatabaseConfig>();
var singleConnectionString = databaseConfig?.SingleConnectionString
.Replace("{AppDir}", AppDomain.CurrentDomain.BaseDirectory)
.Replace("\\", "/"); // value: "Data Source=/data/user/0/com.companyname.cbt.maui.blazor/files/psyonicru_localdb.sqlite"
builderServices.AddDbContextFactory<CBTIdentityDataContext>(options =>
{
options.UseSqlite(singleConnectionString);
}, ServiceLifetime.Transient);
builderServices.AddDbContextFactory<CBTDataContext>((options) =>
{
options.UseSqlite(singleConnectionString);
}, ServiceLifetime.Transient);
}
public class CBTIdentityDataContext : IdentityDbContext<User, Role, string, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
public CBTIdentityDataContext(DbContextOptions<CBTIdentityDataContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
#region Identity
builder.Entity<User>().ToTable("_AspNetUser", "dbo"); // schema name ignored by SQLite provider, not the problem
builder.Entity<Role>().ToTable("_AspNetRole", "dbo");
builder.Entity<RoleClaim>().ToTable("_AspNetRoleClaim", "dbo");
builder.Entity<UserClaim>().ToTable("_AspNetUserClaim", "dbo");
builder.Entity<UserRole>().ToTable("_AspNetUserRole", "dbo");
builder.Entity<UserLogin>().ToTable("_AspNetUserLogin", "dbo");
builder.Entity<UserToken>().ToTable("_AspNetUserToken", "dbo");
#endregion
}
}
An error appears in output log:
SQLite Error 1: 'no such table: _AspNetUser'.
Specified table exists in database file, I opened it with SQLite studio and checked.
What I need to do to fix this issue?