【发布时间】:2012-11-23 05:52:36
【问题描述】:
我的 SQLite 数据库中有一个简单的表:
CREATE TABLE ProductCategories
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name NVARCHAR(128) NOT NULL UNIQUE,
ImageUrl NVARCHAR(256),
ParentCategoryId INTEGER,
FOREIGN KEY (ParentCategoryId) REFERENCES ProductCategories(Id) ON UPDATE CASCADE ON DELETE CASCADE
);
我使用 Devart 的 SQLite ADO.NET 提供程序的免费版本。它工作正常,我考虑使用 Dapper ORM,但类型映射有点问题。 对于 ProductCategories 表,我有这个类:
public class ProductCategory
{
public Int32 Id { get; set; }
public String Name { get; set; }
public String ImageUrl { get; set; }
public Int32? ParentCategoryId { get; set; }
}
所以如果我尝试这样的事情:
var categories = connection.Query<ProductCategory>("SELECT * FROM ProductCategories");
foreach (var c in categories)
{
Console.WriteLine(c.Id + " " + c.Name + " " + c.ImageUrl + " " + c.ParentCategoryId);
}
由于表中的 ParentCategoryId 字段无法转换为 Int32,我得到了一个执行结果?父类别 ID。 此外,如果我使用 ADO.NET ExecuteReader,我总是可以检查该字段是否可以为空,如果未设置外键,则该字段为空。 所以我正在为这个表寻找合适的数据映射。
【问题讨论】:
-
一般情况下,当 dapper 投射失败时,它会告诉你源数据的类型。它有什么说法吗?或者,既然您提到了原始 ado.net:GetFieldType() 为该 col-index 返回什么?
-
我发现了一个问题。 SQLite INTEGER 是 64 位的,但我使用的是 Int32?父类别。所以我将表中的 INTEGER 类型更改为 INT,现在一切正常。
-
也许可以将其添加为答案