0
我在我的SQLite數據庫有一個簡單的表:小巧玲瓏的ORM和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);
}
我得到一個execption,因爲從表中ParentCategoryId字段不能澆鑄的Int32 ? ParentCategoryId。 另外,如果我使用ADO.NET ExecuteReader,我總是可以檢查該字段是否爲空,如果未設置外鍵,則爲空。 所以我正在尋找適合此表的數據映射。
通常,當短小精靈無法投射時,它會告訴您源數據的類型。它會說什麼嗎?或者,由於您提到了原始ado.net:GetFieldType()爲該col-index返回了什麼? –
我發現了一個問題。 SQLite的INTEGER是64位,但我用Int32? ParentCategory。所以我將表格中的INTEGER類型改爲INT,現在一切正常。 – shadeglare
可能會添加作爲答案 –