dotnet / dotnet/efcore

builder.Services.AddDbContext not working when iOS app runs on Mac M1

Open
#32,257 2 comments 0 reactions 0 assignees View on GitHub
area-adonet-sqlite customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Hi
It took a while to understand what the error is as it is not possible to run debug and get this error. I had to Release to TestFlight and install on the Mac M1 computer for it to occur. If I installed on a iPhone or simulator, both approach works.

this code crash on Mac M1
```
builder.Services.AddDbContext(
options => options.UseSqlite($"Filename={GetDataBasePath()}", x => x.MigrationsAssembly(nameof(MySolarCellsSQLite))));

public static string GetDataBasePath()
{
string databasePath ="";
string databaseName = "Db_v_1.db3";
if (DeviceInfo.Platform == DevicePlatform.Android)
databasePath = Path.Combine(FileSystem.AppDataDirectory, databaseName);
else if (DeviceInfo.Platform == DevicePlatform.iOS)
{
SQLitePCL.Batteries_V2.Init();
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library");
}
var dbPath = Path.Combine(databasePath, "Db_v_1.db3");
return dbPath;
}

```

but if I change to this instead in the dbcontext class it works
on both Mac m1 and Phone.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

string fodlderPath = FileSystem.AppDataDirectory;
var dbPath = Path.Combine(fodlderPath, "Db_v_1.db3");
try
{
optionsBuilder.UseSqlite($"Filename={dbPath}");
}
catch
{

}
}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.