【问题标题】:How can I set a default for a column with the SQLITE version used in Xamarin?如何为 Xamarin 中使用的 SQLITE 版本的列设置默认值?
【发布时间】:2017-10-12 17:00:21
【问题描述】:

我正在我的 Xamarin 应用程序中创建一个 SQLITE 表,如下所示:

public class ClickHistory
{
    [PrimaryKey, NotNull]
    public string Yymmdd { get; set; }
    public int DayOfYear { get; set; }
    public int Year { get; set; }
    public int Month { get; set; }
    public int Day { get; set; }
    public int BtnACount { get; set; }
    public int BtnBCount { get; set; }
    public int BtnCCount { get; set; }
    public int BtnDCount { get; set; }
}

db2.CreateTable<ClickHistory>();

谁能帮助我,告诉我如何将 BtnACount、BtnBCount、BtnCCount、BtnDCount 列的默认值设置为 0。现在,当我执行插入但不指定这些列时,它默认为 null .

请注意在另一个问题中建议的解决方案,一个人为我标记为重复:

[NotNull, Default(value: true)]

在带有 Xamarin 的 SQLITE 版本中不起作用

【问题讨论】:

  • 如果你使用它会得到什么?
  • 您使用的是哪个特定的包名称/版本?
  • 你有解决办法吗?

标签: xamarin xamarin.forms


【解决方案1】:

如果您的 SQLite 支持 Default,则使用 Default 属性

[Table("blabla")]
public class Blabla {
    [PrimaryKey, AutoIncrement, NotNull]
    public int id { get; set; }

    [NotNull,Default(value:1)]
    public int bleh { get; set; }
}

如果你的 SQLite 不支持默认值,你可以像这样简单地分配默认值:

[Table("blabla")]
    public class Blabla {
        [PrimaryKey, AutoIncrement, NotNull]
        public int id { get; set; }

        [NotNull]
        public int bleh { get; set; } = 1;
    }

我更喜欢第二种方式。

【讨论】:

    猜你喜欢
    • 2015-01-09
    • 2016-04-27
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2011-12-03
    相关资源
    最近更新 更多