首页 > 解决方案 > Xamarin、Android、PCL SQLLite、SQLite.NET 混乱

问题描述

在网上搜索时,很难理解要在 Xamrin 解决方案中安装的 nuget 包,有几十个包,几十个不同的解决方案。

目前,我们的解决方案有 2 个项目,一个是 Android 项目,一个是 PCL 项目。我们的模型和数据访问在 PCL 中定义。我们的平台实现是在 Android 中定义的。

我们需要 SQLite、SQLite.Net(用于数据注释和表关系)和用于 *withchildren 方法的 SQLiteExtentions。

我们被困在旧版本中,因为每当我们尝试更新任何东西时,我们安装的软件包协同工作的脆弱神奇方式就会崩溃。我们确实需要升级或找到一种方法将 SQLCipher 添加到这个奇怪的 nuget 包中。

我们当前安装的软件包,它的工作原理:

安卓项目

PCL 项目(模型定义和数据访问方法)

目前,如果我们将 SQLiteExtensions 更新到 2.0,则会安装一堆其他 SQLite nuget 包,这会破坏我们数据访问代码的脆弱稳定性(在 *WithChildren 方法上失败:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1929  'SQLiteConnection' does not contain a definition for         
'GetWithChildren' and the best extension method overload 
'ReadOperations.GetWithChildren<TEntity>(SQLiteConnection, object, bool)' 
requires a receiver of type 'SQLiteConnection'  

我们还需要合并 SQLiteCipher,并且无法使任何包组合与我们的解决方案一起使用。

我们的 Android 平台特定实现:

#region Usings

using OURPCLLib.DataAccess;
using Serilog;
using SQLite.Net;
using SQLite.Net.Interop;
using SQLite.Net.Platform.XamarinAndroid;
using System;
using System.IO;

#endregion Usings

public class AndroidSQLiteDatabase : SQLiteDatabaseAccess
{
    protected ISQLitePlatform SQLitePlatform
    {
        get { return new SQLitePlatformAndroidN(); }
    }

    protected override SQLiteConnection GetConnection()
    {
        var conn = new SQLiteConnection(
            SQLitePlatform,
            "dbpathforus.sqlite",
            SQLiteOpenFlags.ReadWrite |
            SQLiteOpenFlags.FullMutex |
            SQLiteOpenFlags.ProtectionCompleteUnlessOpen |
            SQLiteOpenFlags.Create |
            SQLiteOpenFlags.SharedCache);

        return conn;
    }

}

PCL 中的(简化的)基本数据访问类:

#region Usings

using OURPCLLib.DataAccess.Entities;
using SQLite.Net;
using SQLite.Net.Attributes;
using SQLite.Net.Interop;
using SQLiteNetExtensions.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;

#endregion Usings

public abstract class SQLiteDatabaseAccess
{
    protected abstract SQLiteConnection GetConnection();

    // Example of one of the many methods accessing the DB using SQLite.Net
    public bool Any<TEntity>(Expression<Func<TEntity, bool>> expression)
       where TEntity : class, IBaseEntity, new()
    {        
        using (var currentConnection = this.GetConnection())
        {
            return currentConnection.Table<TEntity>().Where(expression).FirstOrDefault() != null;
        }
    }

    // Example of one of the methods accessing the DB using SQLiteExtentions
    public TEntity GetWithChildren<TEntity>(int id, bool recursive = false)
        where TEntity : class, IBaseEntity, new()
    {
        using (var currentConnection = this.GetConnection())
        {
            return currentConnection.GetWithChildren<TEntity>(id, recursive);
        }
    }
}

任何人都可以帮助我们如何在像我们这样的项目中使用 SQLite 与 SQLIte.net、SQLiteExtentions 和 SQLIte 密码?(pcl中的数据访问和android项目中的连接实现?

标签: androidxamarinportable-class-librarysqlite.netsqlite-cipher

解决方案


对于任何想知道的人,我通过仅安装 SQLiteNetExtensions 并让它自己获得其 SQLite 依赖关系,以最小的代码影响和一些功能损失但不多的方式解决了它。


推荐阅读