android - Xamarin、Android、PCL SQLLite、SQLite.NET 混乱
问题描述
在网上搜索时,很难理解要在 Xamrin 解决方案中安装的 nuget 包,有几十个包,几十个不同的解决方案。
目前,我们的解决方案有 2 个项目,一个是 Android 项目,一个是 PCL 项目。我们的模型和数据访问在 PCL 中定义。我们的平台实现是在 Android 中定义的。
我们需要 SQLite、SQLite.Net(用于数据注释和表关系)和用于 *withchildren 方法的 SQLiteExtentions。
我们被困在旧版本中,因为每当我们尝试更新任何东西时,我们安装的软件包协同工作的脆弱神奇方式就会崩溃。我们确实需要升级或找到一种方法将 SQLCipher 添加到这个奇怪的 nuget 包中。
我们当前安装的软件包,它的工作原理:
安卓项目
- Mono.Data.Sqlite.Portable 1.0.3.5(不知道为什么......)
- SQLite.Net.Core-PCL 3.1.1
- SQLite.Net.Platform.XamarinAndroidN 3.1.1
- SQLite.Net-PCL 3.1.1
- SQLiteNetExtensions 1.3.0(GetWithChildren 等需要)
- SQLitePCL.raw 0.9.3
PCL 项目(模型定义和数据访问方法)
- Mono.Data.Sqlite.Portable 1.0.3.5(不知道为什么......)
- SQLite.Net.Core-PCL 3.1.1
- SQLite.Net.Platform.XamarinAndroidN 3.1.1
- SQLite.Net-PCL 3.1.1
- SQLiteNetExtensions 1.3.0(GetWithChildren 等需要)
- SQLitePCL.raw 0.9.3
目前,如果我们将 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项目中的连接实现?
解决方案
对于任何想知道的人,我通过仅安装 SQLiteNetExtensions 并让它自己获得其 SQLite 依赖关系,以最小的代码影响和一些功能损失但不多的方式解决了它。
推荐阅读
- c# - 字符串封送处理的问题
- c++ - UnrealEngine4 - 限制 DataTable 指针仅使用某些行结构
- python - 获取二维列表的每个第一个元素
- c++ - 遍历还包含未使用元素的字符串数组
- c# - 依赖注入解析,构造函数具有多个参数,其值在运行时确定 .NET Core 3
- python - 带有映射修改的熊猫
- python - 如何使用不是棉花糖中有效 Python 名称的键反序列化 JSON
- raspberry-pi3 - 具有 P2P 连接的 IoT 管理页面
- c# - Xamarin.Forms MediaManager 从本地存储播放视频
- c# - 每次单击保存时将列表绑定到 DataGridView 不显示任何内容并清除对象