Reputation: 3681
I am getting below Exception when I try to read or write data to local DB on my MAUI application.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' at MyApp.SqlHelper.GetConnection() in /Users/MyCompany/Projects/MyProject-maui/MyProject/Renderers/SqlHelper.cs:line 28 at MyApp.SqlHelper..ctor() in /Users/MyCompany/Projects/MyProject-maui/MyProject/Renderers/SqlHelper.cs:line 18 at MyProject.App.get_Database() in /Users/MyCompany/Projects/MyProject-maui/MyProject/App.xaml.cs:line 92
I have installed below packages:
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="SQLiteNetExtensions.Async" Version="2.1.0" />
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.6" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.6" />
<PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.6" />
<PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.6" />
My Code:
App.xaml.cs
public static SqlHelper Database
{
get
{
if (database == null)
{
database = new SqlHelper();
}
return database;
}
}
SqlHelper.cs
public SqlHelper()
{
database = GetConnection();//Exception is on this line
database.CreateTable<Favorites>();
}
It is working fine on Android platform and issue is only on iOS platform.
Update
public SQLite.SQLiteConnection GetConnection()
{
SQLiteConnection sqlitConnection;
var sqliteFilename = "Favorites.db3";
IFolder folder = FileSystem.Current.LocalStorage;
string path = PortablePath.Combine(folder.Path.ToString(), sqliteFilename);
sqlitConnection = new SQLite.SQLiteConnection(path);
return sqlitConnection;
}
Upvotes: 0
Views: 259
Reputation: 8090
The error thrown in the following line was caused by PCLStorage NuGet, which is not compatible with .NET7-iOS.
IFolder folder = FileSystem.Current.LocalStorage;
So as an alternative, you could use File system helpers.
You may try the following code,
using FileSystem = Microsoft.Maui.Storage.FileSystem;
.....
public SQLite.SQLiteConnection GetConnection()
{
SQLiteConnection sqlitConnection;
var sqliteFilename = "Favorites.db3";
string path = Path.Combine(FileSystem.AppDataDirectory,sqliteFilename);
sqlitConnection = new SQLite.SQLiteConnection(path);
return sqlitConnection;
}
Upvotes: 1
Reputation: 123
Have you tried following the docs? Their code specifies a database path and this should usually work.
public static class Constants
{
public const string DatabaseFilename = "TodoSQLite.db3";
public const SQLite.SQLiteOpenFlags Flags =
// open the database in read/write mode
SQLite.SQLiteOpenFlags.ReadWrite |
// create the database if it doesn't exist
SQLite.SQLiteOpenFlags.Create |
// enable multi-threaded database access
SQLite.SQLiteOpenFlags.SharedCache;
public static string DatabasePath => Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
}
SQLiteAsyncConnection = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
var result = await Database.CreateTableAsync<Favorites>();
Upvotes: 0