我有一个数据模型,其中对象的子引用可能为空(即可能不在帐户上设置辅助地址)。当我在父实体上尝试LoadSelect
时,我收到ArgumentNullException: Value cannot be null.
错误,表面上是在加载子引用时。
鉴于这种数据情况有多常见,我是否只是做错了什么?否则,这是LoadListWithReferences
中的缺陷吗?
我已经创建了一个小示例程序来说明这种行为:
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
using System.Collections.Generic;
using System.Data;
namespace SSLoadSelectTest
{
public class Parent
{
[PrimaryKey]
public int Id { get; set; }
[References(typeof(Child))]
public int? ChildId { get; set; }
[Reference]
public Child Child { get; set; }
}
public class Child
{
[PrimaryKey]
public int Id { get; set; }
public string Value { get; set; }
}
class Program
{
static void Main(string[] args)
{
OrmLiteConfig.DialectProvider = SqliteDialect.Provider;
IDbConnection Db = SqliteDialect.Provider.CreateConnection(":memory:", new Dictionary<string, string>());
Db.Open();
Db.CreateTables(true, typeof(Parent), typeof(Child));
Db.Insert<Child>(new Child() { Id = 1, Value = "Hello" });
Db.Insert<Parent>(new Parent() { Id = 1, ChildId = (int)Db.LastInsertId() });
Db.Insert<Parent>(new Parent() { Id = 2, ChildId = null });
var results1 = Db.LoadSelect<Parent>(p => p.Id == 1);
var results2 = Db.LoadSelect<Parent>(p => p.Id == 2);
}
}
}