Circular Links in RavenDB

I am using RavenDB with models such as:

public class Cart {
    List<Item> Items = new List<Item>();
}

public class Item {
    public Cart Cart;
}

When I add Itemin Cart, I connect both sides of the relationship.

How does RavenDB handle serializing and deserializing this? Will a link be assigned to Cartautomatically, or is there a way to manually connect it at boot time?

+5
source share
2 answers

If you save the recycle bin, you will get the "Autodrome Discovered" exception ... when called SaveChanges().

Easy to fix. You add the attribute [JsonObject(IsReference = true)]to the cart. This says the serializer saves the Item Cart as a link instead of a new object.

[JsonObject(IsReference = true)] 
public class Cart
{
    public List<Item> Items = new List<Item>();
}

public class Item
{
    public Cart Cart;
}

, . , .

var cart = new Cart();
cart.Items.Add(new Item { Cart = cart });
session.Store(cart);
session.SaveChanges();

...

var cart = session.Query<Cart>().First();
Assert.ReferenceEquals(cart, cart.Items.First().Cart); //this works
+9
0

All Articles