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:
- If I swap the order of Branches and Meta properties in the User class, the exception goes away.
- If I uncomment the Addresses property and add it to the user, the exception returns.
Questions:
- I find it odd that the order of properties would affect the results (am I doing something wrong)?
- 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; }
}
}