0

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?

5
  • an Embedded Resource is not a file. You will need to move it to a writable location at runtime before connecting to it Commented Jun 21 at 15:14
  • @Jason string '/data/user/0/com.companyname.cbt.maui.blazor/files/psyonicru_localdb.sqlite' looks like executable location. Is this guide correct for my situation (means /data/data...): stackoverflow.com/a/24824482/571203 ? Commented Jun 21 at 20:07
  • I had this issue, pretty sure Andriod ended up looking in a second location, I just deleted the Db. You can use Andriod Studio to verify the Db Location/Last Update time was Commented Jun 22 at 9:28
  • Also had same issue, turns out database not copied to android storage. you might need to put it in Resources/Raw and copy to AppDataDirectory on first run and don't use AppDomain path, it doesn't work on android. Commented Jun 23 at 13:10
  • @ShirazAhmed can you describe this approach in answer to question? I solved my problem somehow, but not sure about usage in real Android environment with future updates of application. Commented Jul 18 at 20:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.