.NET MAUI SQLite CreateTableAsync应用程序无一例外地崩溃

91zkwejq  于 2023-03-08  发布在  SQLite
关注(0)|答案(2)|浏览(170)

我目前正在尝试在我的MAUI项目中使用SQLite。我以前在WPF中使用过它,没有任何问题,我在MAUI中也是以完全相同的方式使用它,但没有成功。
网上所有的解决方案对我都不起作用,也许有人能帮我解决。
下面是我的代码:
我调用createProfiles()ProfileService.AddProfile(Profile)调用ProfileService.AddProfile(Profile)ProfileService.AddProfile(Profile)调用Init()。在Init()中,我的应用程序运行到await db.CreateTableAsync<Profile>();,然后关闭,没有抛出任何异常。

    • 配置文件服务. cs**
public class ProfileService : IProfileService
{
    SQLiteAsyncConnection db;

        async Task Init()
        {
            if (db != null)
                return;

            var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Profiles.db");

            db = new SQLiteAsyncConnection(databasePath);

            await db.CreateTableAsync<Profile>();
        }

        public async Task AddProfile(Profile profile)
        {
            await Init();
            var id = await db.InsertAsync(profile);
        }
}
    • 视图模型**
private async void createProfiles()
        {
            Profile tempProfile = new Profile
            {
                Name = "Test",
                Hostname = "0.0.0.0",
                Port = 23,
            };

            await ProfileService.AddProfile(tempProfile);
        }
    • 配置文件. cs**
public class Profile
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Hostname { get; set; }
        public int Port { get; set; }

        public Profile()
        {

        }
    }

我试过在Profile构造函数中设置默认值,但没有成功。我还试过将await ProfileService.AddProfile(tempProfile); Package 在一个单独的线程中,但没有成功。

7fhtutme

7fhtutme1#

public interface IProfileRepo
        {
            Task<bool> AddProfileAsync(Profile profile);    
        }    

  public class ProfileRepo : IProfileRepo
    {
      private readonly SQLiteAsyncConnection _connect;
            public ProfileRepo (SQLiteAsyncConnection connect)
            {
                _connect = connect;
                Task.Run(() => this.CreateTable()).Wait();
            }
    
            private async Task CreateTable()
            {
                await _connect.CreateTableAsync<Profile>();
            }
            //Other methods Add, Delete and etc.
    }

    public class UnitOfWork
        {
            private readonly SQLiteAsyncConnection _connect;
            private IProfileRepo _profileRepo;
            public UnitOfWork(string dbPath)
            {
                _connect = new SQLiteAsyncConnection(dbPath);
            }
            public IProfileRepo ProfileRepo
            {
                get
                {
                    return _profileRepo = _profileRepo ?? new ProfileRepo(_connect);
                }
            }      
        }

public class ProfileService
{
        public async Task Add(Profile profile)
        {
          await App.UnitOfWork.ProfileRepo.Add(profile);
        }
}

public partial class App : Application
{
public static UnitOfWork UnitOfWork
        {
            get
            {
                if (unitOfWork == null)
                {
                    unitOfWork = new UnitOfWork(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TestDB.db3"));
                }
                return unitOfWork;
            }
        }
}

在开发过程中,您可能希望在每次启动应用程序时删除数据库。

private void DeleteLocalDB()
        {           
            var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TestDB.db3");
            if(File.Exists(file))
            {
                File.Delete(file);
            }            
        }

使用的熔核 Package

<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
4smxwvx5

4smxwvx52#

你是否遵循了microsoft docs中的指南?你需要一个额外的SQLitePCLRaw.bundle_green库来使sqlite-pcl-net在每个平台上工作。

相关问题