I’m trying to figure-out the simplest way to persist a complex(2 level deep) object as this one
public class Root
{
[AutoIncrement]
public int RootId { get; set; }
[ServiceStack.DataAnnotations.Reference]
public List<Item> Items { get; set; }
}
public class Item
{
[AutoIncrement]
public int ItemId { get; set; }
public int RootId { get; set; } //`{Parent}Id` convention to refer to Client
public string MyValue { get; set; }
}
That what I’d like to accomplish
int rootId = 0;
var root = new Root { Items = new List<Item> { new Item { MyValue = "x" } } };
using (var db = _dbConnectionFactory.Open(_namedConnectionString))
{
db.Store(root, references: true);
}
root.RootId.Should().BeGreaterThan(0);// from autoincrement column
root.Items.First().ItemId.Should().BeGreaterThan(0);// from autoincrement column
rootId = root.RootId;
// disconnected scenario
var root2 = new Root { RootId = rootId, Items = new List<Item> { new Item { RootId = rootId, MyValue = "y" }, new Item { RootId = rootId, MyValue = "z" } } };
using (var db = _dbConnectionFactory.Open(_namedConnectionString))
{
db.Store(root, references: true);
}
root.Items.Should().HaveCount(2); // MyValue = "x" should be been deleted
root.Items.Count(x => x.MyValue == "y").Should().Be(1);
root.Items.Count(x => x.MyValue == "z").Should().Be(1);
In other world I’d like to avoid to handle the ItemId, have all my existing Items deleted and have the new items added (of course with a new ItemId): I do not want to have to explicitly delete the existing items but those should be automatically deleted since the new graph does not contain the old one. The store method should figure out what has to be deleted, what has to be added and what to be updated only according to the foreign key.