xamarin - 如何在 sqlite.net PCL 中使用 InsertOrReplace?

标签 xamarin sqlite.net

我正在使用这里的 sqlite.net 的 PCL 版本( https://github.com/oysteinkrog/SQLite.Net-PCL )。

这是我的简单类(class)。

    public class LogEntry
    {
      [PrimaryKey, AutoIncrement]
      public int Key { get; set;}
      public DateTime Date { get; set; }
    }

创建 LogEntry 的新实例时,Key 会自动设置为 0。我将 Date 设置为某个值,然后调用 InsertOrReplace。该记录确实保存在我的数据库中。 Key 字段获取自动增量值,该值恰好为 0,因为它是第一条记录。

然后我创建一个新的 LogEntry 实例(Key 自动初始化为 0)并将日期设置为其他内容。然后我调用 InsertOrReplace。由于存在 Key 为 0 的现有记录,该记录将被更新。

处理这个问题的正确方法是什么?我考虑将 Key 初始化为 -1,但这似乎也不起作用。

有没有人有这个工作的例子?

最佳答案

如果您将 Key 更改为可空类型(int?),它应该可以工作。然后 SQLite 看到 null 传入并在需要时生成新的 id。

public class LogEntry
    {
      [PrimaryKey, AutoIncrement]
      public int? Key { get; set;}
      public DateTime Date { get; set; }
    }

关于xamarin - 如何在 sqlite.net PCL 中使用 InsertOrReplace?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409870/

相关文章:

ios - Xamarin iOS后台上传任务(iOS 7)——DidFinishEventsForBackgroundSession

c# - sqlite-net-pcl 中的 InsertAll 和 UpdateAll VS SQLite.Net-PCL 中的 InsertOrReplaceAll

android - Xamarin SQLite.NET 通用表查询

c# - 不能同时使用 WP8.1 项目中引用的 System.IO.Compression 和 SQLite

ios - 使用 Monotouch 修剪视频失败并显示 "The operation could not be completed"

Android 上的 C# : Xamarin or Unity?

c# - 如何将 ListView 设置为开始显示最后一个项目而不是在 Xamarin Forms 中?

ios - Xamarin IOS 自定义 URL 方案在 iOS 14 中不起作用

c# - sqlite.net 表 where 子表上的条件

c# - 是否可以将 SQLite.NET 与不可变记录类型一起使用?