【发布时间】:2015-07-18 03:54:00
【问题描述】:
我使用 Xamarin for Visual Studio 2013 和 SQLite-net PCL 版本 1.0.11 (https://www.nuget.org/packages/sqlite-net-pcl/)。
基本上,我有兴趣在 SQLite 表中存储大于 2,147,483,647 的数值,其中字段/列通过 ORM 对类进行 [PrimaryKey, AutoIncrement] 装饰。
下面的类确实创建了表,并且插入值(行)工作正常:
public class Patient
{
[PrimaryKey, AutoIncrement, Column("PATIENT_NBR")]
public int PatientNumber { get; set; }
...
}
当“long”或“Int64”与 PrimaryKey 和 AutoIncrement 结合使用时,以下两个类不会创建表:
public class Patient
{
[PrimaryKey, AutoIncrement, Column("PATIENT_NBR")]
public long PatientNumber { get; set; }
...
}
或
public class Patient
{
[PrimaryKey, AutoIncrement, Column("PATIENT_NBR")]
public Int64 PatientNumber { get; set; }
...
}
在上面的最后两个示例中 - 运行 CreateTable 时:
public class CreateDatabase
{
private readonly SQLiteConnection conn;
public CreateDatabase()
{
try
{
conn = DependencyService.Get<ISQLite>().GetConnection(Constants.DatabaseName);
conn.CreateTable<Patient>();
if (conn.Table<Patient>().Count() == 0)
{
conn.Insert(new Patient { PatientCreateDate = DateTime.UtcNow, PatientFirstName = "John", PatientLastName = "Doe" });
conn.Insert(new Patient { PatientCreateDate = DateTime.UtcNow, PatientFirstName = "Jane", PatientLastName = "Doe" });
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Create SQLite Connection. Error: {0}.", ex.Message));
}
}
}
我收到异常: ex.Message:“AUTOINCREMENT 只允许在 INTEGER PRIMARY KEY 上”
如果我从 PatientNumber 的装饰中删除 AutoIncrement,它将很好地创建表,但随后我遇到约束错误,因为插入 PatientNumber 的值始终为 0(因为未使用 AutoIncrement),并且该字段被定义为 PrimaryKey 并且必须是唯一的。
基于为 SQLite-net 打开的问题:
https://github.com/praeclarum/sqlite-net/issues/227
它提到了以下内容:
https://github.com/praeclarum/sqlite-net/pull/345
(很抱歉上面的两个链接 - 它不会让我发布它们)
在上面的最后一个链接中,它提到了一种解决方法:
conn.ExtraTypeMappings.Add(typeof(int64), "integer")
但我不确定在哪里添加这个。我尝试在分配“conn”的位置下方添加它,但出现以下编译器错误: “SQLite.SQLiteConnection 不包含“ExtraTypeMapping”的定义。
我的理解是,虽然 SQLite 可以存储最大为 9223372036854775807 的数值,但我会受到“Int”或“Int32”类型的 C# 类的修饰的限制,因为它的最大值是 2,147,483,647。
是我遗漏了什么,还是我误解了什么??
任何帮助将不胜感激。
【问题讨论】:
标签: visual-studio-2013 orm xamarin portable-class-library sqlite-net