将 SQLiteNetextensions 与 Xamarin for Android-App 结合使用 [英] Use SQLiteNetextensions with Xamarin for Android-App

查看:26
本文介绍了将 SQLiteNetextensions 与 Xamarin for Android-App 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于 Xamarin 的 SQLite 的主题,但我仍然无法以正确的方式设置 SQLiteNetExtensions.我目前正在使用 Target Framework Android 7.1(API 级别 25 - Nougat)开发一个 Android 应用.

I've read some threads about SQLite for Xamarin but I'm still not able to set up SQLiteNetExtensions in a proper way. I'm currently developing an android app with Target Framework Android 7.1(API Level 25 - Nougat).

谁能告诉我我做错了什么?

Can anybody tell me what I'm doing wrong?

  1. 我安装了 nuget 包:

  1. I installed nuget packages:

Install-Package SQLiteNetExtensions -Version 2.0.0-alpha2 -Pre

安装包 SQLite.Net-PCL -Version 3.1.1

根据:https://bitbucket.org/twincoders/sqlite-net-extensions

然后我设置了我的代码.

Then I set up my code.

using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;

namespace AppName.Resources.Model
{
  public class Entry
  {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [ForeignKey(typeof(Stock))]
    public int StockId { get; set; }
    public DateTime Date { get; set; }
    public double Amount { get; set; }
  }
}


using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
using System.Collections.Generic;

namespace AppName.Resources.Model
{
  public class Stock
  {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime LastUpdated { get; set; }
    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<Entry> Entrys { get; set; }
  }
}


using Android.Util;
using AppName.Resources.Model;
using SQLite.Net;
using SQLite.Net.Platform.XamarinAndroid;
using SQLiteNetExtensions.Extensions;
using System.Collections.Generic;
using System.IO;

namespace AppName.Resources.DataHelper
{
  public class DataBase
  {
    string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    public bool CreateDataBase()
    {
      try
      {
        using (var stocksDBConnection = new SQLiteConnection(new SQLitePlatformAndroid(), Path.Combine(folder, "Stock.db")))
        {
          stocksDBConnection.CreateTable<Entry>();
          stocksDBConnection.CreateTable<Stock>();    
          return true;
        }
      }
      catch (SQLiteException ex)
      {
        Log.Info("SQLiteEx", ex.Message);
        return false;
      }
    }

    public bool InsertIntoTableStock(object stock)
    {
      try
      {
        using (var stocksDBConnection = new SQLiteConnection(new SQLitePlatformAndroid(), Path.Combine(folder, "Stock.db")))
        {
          stocksDBConnection.InsertWithChildren(stock);
          return true;
        }
      }
      catch (SQLiteException ex)
      {
        Log.Info("SQLiteEx", ex.Message);
        return false;
      }
    }
    ...

  • 这些引用是由 nuget 添加的:

  • These References were added by nuget:

    SQLite-net

    SQLite.Net

    SQLite.Net.Platform.XamarinAndroid

    SQLite.Net.Platform.XamarinAndroid

    SQLiteNetExtensions

    SQLiteNetExtensions

    SQLitePCLRaw.Batteries_green

    SQLitePCLRaw.Batteries_green

    SQLitePCLRaw.Core

    SQLitePCLRaw.Core

    SQLitePCLRaw.lib.e_sqlite3

    SQLitePCLRaw.lib.e_sqlite3

    SQLitePCLRaw.provider.e_sqlite3

    SQLitePCLRaw.provider.e_sqlite3

  • 发生错误:

  • Occuring error:

    SQLiteConnection"不包含InsertWithChildren"的定义,最好的扩展方法重载WriteOperations.InsertWithChildren(SQLiteConnection, object, bool)"需要一个SQLiteConnection"类型的接收器

    'SQLiteConnection' does not contain a definition for 'InsertWithChildren' and the best extension method overload 'WriteOperations.InsertWithChildren(SQLiteConnection, object, bool)' requires a receiver of type 'SQLiteConnection'

    'SQLiteConnection' 不包含 'GetAllWithChildren' 的定义,并且最好的扩展方法重载 'ReadOperations.GetAllWithChildren(SQLiteConnection, Expression>, bool)' 需要一个类型为 'SQLiteConnection' 的接收器

    'SQLiteConnection' does not contain a definition for 'GetAllWithChildren' and the best extension method overload 'ReadOperations.GetAllWithChildren(SQLiteConnection, Expression>, bool)' requires a receiver of type 'SQLiteConnection'

  • 嗯,这就是我得到的.有谁知道该怎么做吗?也许删除冲突的引用?

    Well that's how far I get. Anybody out there who knows what to do? Maybe remove conflicting references?

    推荐答案

    经过一番研究,我发现:

    After some research I found out that:

    Install-Package SQLiteNetExtensions -Version 2.0.0-alpha2 -Pre
    

    取决于

    Install-Package sqlite-net-pcl
    

    代替

    Install-Package SQLite.Net-PCL -Version 3.1.1
    

    但是在删除所有引用、清理解决方案并重新安装两个需要的包之后,就没有 SQLite.net 可用了.当尝试使用 SQLite 代替时:

    But after removing all references, cleaning the solution and reinstalling the two needed packages there is no SQLite.net available anymore. When trying to use SQLite instead:

    SQLiteConnection"不包含InsertWithChildren"的定义,并且最好的扩展方法重载WriteOperations.InsertWithChildren(SQLiteConnection, object, bool)"需要一个SQLiteConnection"类型的接收器

    'SQLiteConnection' does not contain a definition for 'InsertWithChildren' and the best extension method overload 'WriteOperations.InsertWithChildren(SQLiteConnection, object, bool)' requires a receiver of type 'SQLiteConnection'

    我很好奇是否可以使用 VS 无缝开发 Android 应用程序.因为一切都需要数年时间才能完成.

    I'm curious if it's even possible to develop Android-Apps with VS seamlessly. Since everything takes years to set up.

    好吧,在 a** 痛苦了几天之后,解决方案是使用更新的预发布版本 (alpha3),官方页面上甚至没有提到该版本.目前我很满意.:)

    Well, after some days of pain in the a** the solution was to use the even newer pre release (alpha3) which wasn't even referenced to on the official pages. Currently I'm satisfied. :)

    Install-Package SQLiteNetExtensions-netstandard -Version 2.0.0-alpha3 -Pre
    

    希望任何人都可能需要这个.

    Hope anybody may need this.

    这篇关于将 SQLiteNetextensions 与 Xamarin for Android-App 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆