Search code examples
servicestackormlite-servicestack

ServiceStack LoadSelect throws ArgumentNull when child reference is null


I have a data model where child references of an object may be null (i.e. a secondary address may not be set on an account). When I attempt to LoadSelect on the parent entity, I receive an ArgumentNullException: Value cannot be null. error, ostensibly when the child references are being loaded.

Given how common this data scenario is, am I just doing something wrong? Else, is this a defect in LoadListWithReferences?

I've created a small sample program to illustrate this behavior:

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);
        }
    }
}

Solution

  • This issue has now been fixed with this commit.

    This fix is available from v4.0.32+ that's now available on MyGet.