【发布时间】:2014-09-18 02:20:55
【问题描述】:
我有一个数据模型,其中对象的子引用可能为空(即,可能未在帐户上设置辅助地址)。当我尝试在父实体上 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);
}
}
}
【问题讨论】:
标签: servicestack ormlite-servicestack