SQLite.Net,SQLite 位数据类型的 C# 等价物
SQLite.Net, C# equivalent for SQLite bit datatype
我在 c#
中声明以下 class
[Table("Employee")]
public class Employee
{
[PrimaryKey,AutoIncrement]
public int EmployeeId { get; set; }
public DateTime DateOfJoining { get; set; }
public string Address{ get; set; }
}
我调用此方法在我的 SQLite 数据库中创建等效的 table
public async Task CreateTable()
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(path);
await conn.CreateTableAsync<Employee>();
}
所以它在SQLite中创建了一个table如下
[EmployeeId] int,
[DateOfJoining] [datetime],
[CallType] [varchar]
我想创建一个列,它是位
[IsActive] [bit]
为此我尝试了
public bool IsActive { get; set; }
和
public Boolean IsActive { get; set; }
这两个属性都会生成一个整数列
[IsActive] integer
那么我应该如何声明我的 IsActive 属性 以获得一个数据类型为 bit 的列。
我还有一个问题,如果我声明属性并指定它不为空
[NotNull]
public bool Address{ get; set; }
然后当我调用 CreateTable() 时它给我一个错误说 "No default value specified for Not Null attribute"。
我试图在构造函数中初始化此 属性,但没有成功。
我该如何解决这些问题?
请帮忙
据我在 SQLite 文档中所见,SQLite 没有位数据类型
并且在 SQLite.Net 中所有类型的 byte/boolean/ints 都映射到整数:参见 this line
对于 NotNull 错误,让我猜猜:
- 您的 Table
中已经有一些条目
- 您现在添加一个具有 NotNull-Attribute 的新列
- SQLite 试图改变 tablet 但它崩溃了,因为新列的多个条目现在具有空值
我认为这是唯一需要按此顺序更改 table 的情况:
- 添加不带 NotNull 的列
- 添加一些列值
- 只有一次所有 table 条目都具有该列的值 --> 现在您可以添加 NotNull 属性
如果您的 Table 为空,则 NotNull 参数将从一开始就起作用。
编辑更简单解决方案:
- 添加不带 NotNull 的列
ALTER TABLE Employee ADD COLUMN IsActive integer default 0;
- 现在您可以添加 NotNull 属性并再次调用 CreateTable
[NotNull]
public bool Address{ get; set; }
await conn.CreateTableAsync<Employee>();
我在 c#
中声明以下 class[Table("Employee")]
public class Employee
{
[PrimaryKey,AutoIncrement]
public int EmployeeId { get; set; }
public DateTime DateOfJoining { get; set; }
public string Address{ get; set; }
}
我调用此方法在我的 SQLite 数据库中创建等效的 table
public async Task CreateTable()
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(path);
await conn.CreateTableAsync<Employee>();
}
所以它在SQLite中创建了一个table如下
[EmployeeId] int,
[DateOfJoining] [datetime],
[CallType] [varchar]
我想创建一个列,它是位
[IsActive] [bit]
为此我尝试了
public bool IsActive { get; set; }
和
public Boolean IsActive { get; set; }
这两个属性都会生成一个整数列
[IsActive] integer
那么我应该如何声明我的 IsActive 属性 以获得一个数据类型为 bit 的列。
我还有一个问题,如果我声明属性并指定它不为空
[NotNull]
public bool Address{ get; set; }
然后当我调用 CreateTable() 时它给我一个错误说 "No default value specified for Not Null attribute"。
我试图在构造函数中初始化此 属性,但没有成功。
我该如何解决这些问题? 请帮忙
据我在 SQLite 文档中所见,SQLite 没有位数据类型
并且在 SQLite.Net 中所有类型的 byte/boolean/ints 都映射到整数:参见 this line
对于 NotNull 错误,让我猜猜:
- 您的 Table 中已经有一些条目
- 您现在添加一个具有 NotNull-Attribute 的新列
- SQLite 试图改变 tablet 但它崩溃了,因为新列的多个条目现在具有空值
我认为这是唯一需要按此顺序更改 table 的情况:
- 添加不带 NotNull 的列
- 添加一些列值
- 只有一次所有 table 条目都具有该列的值 --> 现在您可以添加 NotNull 属性
如果您的 Table 为空,则 NotNull 参数将从一开始就起作用。
编辑更简单解决方案:
- 添加不带 NotNull 的列
ALTER TABLE Employee ADD COLUMN IsActive integer default 0;
- 现在您可以添加 NotNull 属性并再次调用 CreateTable
[NotNull]
public bool Address{ get; set; }
await conn.CreateTableAsync<Employee>();