c# - SQLite.Net,SQLite 位数据类型的 C# 等效项

标签 c# sqlite windows-runtime winrt-xaml sqlite.net

我在 C# 中声明以下类

[Table("Employee")]
public class Employee
{
    [PrimaryKey,AutoIncrement]
    public int EmployeeId { get; set; } 
    public DateTime DateOfJoining { get; set; }
    public string Address{ get; set; }
}

我调用此方法在我的 SQLite 数据库中创建等效的表

public async Task CreateTable()
{
   SQLiteAsyncConnection conn = new SQLiteAsyncConnection(path);
   await conn.CreateTableAsync<Employee>();
}

因此它在 SQLite 中创建了一个表,如下所示

[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() 时,它会给出一个错误:“没有为 Not Null 属性指定默认值”。

我尝试在构造函数中初始化此属性,但没有成功。

我该如何解决这些问题? 请帮忙

最佳答案

据我在 SQLite 文档中看到,SQLite 没有位数据类型

在 SQLite.Net 中,所有类型的字节/ bool /整数都会映射到整数:请参阅 this line

对于NotNull错误,让我猜一下:

  • 您的表格中已有一些条目
  • 您现在添加一个具有 NotNull 属性的新列
  • SQLite 尝试更改平板电脑,但它崩溃了,因为新列中的多个条目现在具有空值

我认为这是必须按此顺序更改表的唯一情况:

  • 添加不带 NotNull 的列
  • 添加一些列值
  • 只有当所有表条目都具有该列的值时 --> 现在您可以添加 NotNull 属性

如果您的表为空,则 NotNull 参数将从一开始就起作用。

<小时/>

编辑更简单的解决方案:

  • 添加不带 NotNull 的列
ALTER TABLE Employee ADD COLUMN IsActive integer default 0; 
  • 现在您可以添加 NotNull 属性并再次调用 CreateTable
[NotNull]
public bool Address{ get; set; }

await conn.CreateTableAsync<Employee>();

关于c# - SQLite.Net,SQLite 位数据类型的 C# 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28208685/

相关文章:

c# - NSAutoReleasePool 和异步函数

c# - Discord.NET 获取特定 react 的 react 计数

sql - 将行与某些预定义值以及一些其他表中的值插入到SQL表中

windows-8 - Windows 8 应用程序 : How to pass a parameter on GoBack()?

c# - 按属性和这些属性的有序数组对 IEnumerable<Object> 进行排序

c# - 华氏度和摄氏度之间的转换问题

sql - Ipad新手,熟悉SQL,如何上手

python - 为什么 sqlite3 快捷方式函数称为 "nonstandard"?

javascript - 如何在 Metro Windows 8 中找到应用栏的图标?

.net - 如何确定是否需要分派(dispatch)到 WinRT/Metro 中的 UI 线程?