3

我正在将 Android 代码导入到 windows rt,并且我需要数据在两个应用程序上都兼容。创建我的 sqlite 数据库时,我需要为列设置默认值,执行类似的操作

CREATE TABLE [blabla] ( [id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [bleh] INTEGER NOT NULL DEFAULT -1 )

我以这种方式使用 SQLite-net 编写了我的 C# 代码:

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

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

但我无法将默认值设置为“bleh”。我需要两个数据库相同=/有人可以帮我吗?

谢谢 =)

4

3 回答 3

3

你为什么不使用:

    private int _bleh = 1;
    [NotNull]
    public int Bleh
    {
        get { return _bleh; }
        set { _bleh = value; }
    }

然后 bleh 将始终具有默认值 1 除非更改

于 2014-11-12T17:22:03.350 回答
2

只要没有得到答案,我只是使用 SQL 语句创建了整个数据库,因此不需要通过我的 C# 代码创建数据库。它工作正常!

于 2014-12-04T16:05:45.897 回答
0

我知道已经晚了,但也许这会对像我这样的人有所帮助。如果Default您的 SQLite 支持,则使用Default属性

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

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

如果Default您的 SQLite 不支持,您可以简单地分配默认值,如下所示:

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

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

我总是喜欢第二种方式。

于 2020-04-30T16:21:01.837 回答