Sqlite Exception Based on Order of Properties?

When trying to execute the attached code using ServiceStack.OrmLite (5.4.0) with a SqliteDialect.Provider, an exception occurs at the “db.SaveAsync(user, references:true);” line. The exception is: “The transaction object is not associated with the connection object.” I’ve noticed 2 peculiarities:

  1. If I swap the order of Branches and Meta properties in the User class, the exception goes away.
  2. If I uncomment the Addresses property and add it to the user, the exception returns.

Questions:

  1. I find it odd that the order of properties would affect the results (am I doing something wrong)?
  2. It appears that I’m unable to have two List<> properties associated with an object, is this expected (or am I doing something wrong)?

Thanks!

Code below:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;

namespace SqliteTestCore
{
    class Program
    {
        static void Main(string[] args)
        {
            OrmLiteConnectionFactory dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
            MainAsync(dbFactory).Wait();
        }

        static async Task MainAsync(OrmLiteConnectionFactory dbFactory)
        {
            await CreateTables(dbFactory);
            await CreateUser(dbFactory);
        }

        static async Task CreateTables(OrmLiteConnectionFactory dbFactory)
        {
            using (var db = await dbFactory.OpenAsync())
            {
                db.CreateTableIfNotExists<User>();
                db.CreateTableIfNotExists<UserBranch>();
                db.CreateTableIfNotExists<UserAddress>();
                db.CreateTableIfNotExists<UserMeta>();
            }
        }

        static async Task CreateUser(OrmLiteConnectionFactory dbFactory)
        {
            var userMeta = new UserMeta();
            var user = new User
            {
                Meta = userMeta
            };

            try
            {
                using (var db = await dbFactory.OpenAsync())
                {
                    user.Branches = new List<UserBranch> { new UserBranch { UserId = user.Id }};
                    //user.Addresses = new List<UserAddress> { new UserAddress { UserId = user.Id }};

                    await db.SaveAsync(user, references: true);
                }
                Console.WriteLine("All good.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

    [Alias("Users")]
    public class User
    {
        [AutoId]
        public Guid Id { get; set; }

        [Reference]
        public List<UserBranch> Branches { get; set; }

        [Reference]
        public UserMeta Meta { get; set; }

        //[Reference]
        //public List<UserAddress> Addresses { get; set; }
    }

    [Alias("UserMetas")]
    public class UserMeta
    {
        [PrimaryKey]
        [ForeignKey(typeof(User), OnDelete = "CASCADE", OnUpdate = "CASCADE")]
        [References(typeof(User))]
        public Guid UserId { get; set; }
    }

    [Alias("UserBranches")]
    public class UserBranch
    {
        [AutoId]
        public Guid Id { get; set; }

        [ForeignKey(typeof(User), OnDelete = "CASCADE", OnUpdate = "CASCADE")]
        public Guid UserId { get; set; }

        public string Details { get; set; }
    }

    [Alias("UserAddresses")]
    public class UserAddress
    {
        [AutoId]
        public Guid Id { get; set; }

        [ForeignKey(typeof(User), OnDelete = "CASCADE", OnUpdate = "CASCADE")]
        public Guid UserId { get; set; }

        public string Details { get; set; }
    }
}

I’ve added this to the Test Suite but I’m not able to repro this issue testing against any of the supported databases. Note I’ve made the test reproducible be re-creating the tables in dependent order.

You can try upgrading to the latest v5.4.1 on MyGet to see if that resolves the issue, but my hunch is that since your test wasn’t re-creating the table schemas whilst you’re change them, that you’re working off a dirty schema which no longer matches the table definition, so I’d first try re-creating the tables in your test:

db.DropTable<UserMeta>();
db.DropTable<UserAddress>();
db.DropTable<UserBranch>();
db.DropTable<User>();

db.CreateTable<User>();
db.CreateTable<UserBranch>();
db.CreateTable<UserAddress>();
db.CreateTable<UserMeta>();