当子引用为null时,ServiceStack LoadSelect抛出ArgumentNull

时间:2014-09-17 17:54:27

标签: servicestack ormlite-servicestack

我有一个数据模型,其中对象的子引用可能为空(即可能不在帐户上设置辅助地址)。当我在父实体上尝试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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

此问题现在已been fixed with this commit

此修复程序可从 v4.0.32 + 获得,现在为available on MyGet