Guides
Relationships
Link objects with ordinary properties and collections - parents and children, references by key, cascade delete, and choosing what to load.
A property whose type is another class is a relationship. XOD stores each class in its own table and
links the rows by key, so an object graph goes in with one Insert and comes back with one Find.
Parent and children (one-to-many)
The parent holds a collection marked [Children]. Each child holds the parent’s key and a reference
marked [ParentKey], naming the property that holds that key.
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
[Children]
public List<Employee> Employees { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string FullName { get; set; }
public int DepartmentId { get; set; }
[ParentKey(nameof(DepartmentId))]
public Department Department { get; set; }
}
Insert the parent with its children, and XOD stores them all and fills in each DepartmentId:
db.Insert(new Department
{
Name = "R&D",
Employees = new() { new Employee { FullName = "Omar" }, new Employee { FullName = "Lina" } },
});
The attributes are optional when the names follow the convention: a collection of objects that each
have a property of the parent’s type, or one named after it plus Id (DepartmentId), is treated as
children without [Children] or [ParentKey].
public class Shelf
{
public int Id { get; set; }
public List<Box> Boxes { get; set; } // children, by convention
}
public class Box
{
public int Id { get; set; }
public int ShelfId { get; set; } // the parent's key, by name
public string Label { get; set; }
}
Children are owned: deleting the parent deletes them (see below). Inserting a parent with a child that
another parent already lists throws ReservedChildException; to move a child, update the new parent or the
child’s parent key.
A child with no key has no row to match, so updating its parent writes each such child again: its old
row is deleted and a new one inserted, with their Delete and Insert triggers. Give the child a key,
such as an Id, to have it updated in place.
References by key (many-to-one)
When a row points at another without owning it, keep the key and mark the reference [ForeignKey]:
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
[ForeignKey(nameof(AuthorId))]
public Author Author { get; set; }
}
var author = db.Insert(new Author { Name = "Ibn Khaldun" });
db.Insert(new Book { Title = "Muqaddimah", AuthorId = author.Id });
var book = db.Find<Book>(b => b.Title == "Muqaddimah");
Console.WriteLine(book.Author.Name); // Ibn Khaldun
When the remote key isn’t the other class’s primary key, name it too:
[ForeignKey("AuthorCode", "Code")]. [ParentKey] takes the same two-name form.
A plain object property with neither attribute, like Venue on an Event, is stored in its own table
and linked to the row that holds it.
Each query builds referenced objects afresh from the current stored rows: after updating or deleting the
author, the next query of the book returns the current state, and two queries return separate instances
(see Loading relationships). An object reference to a deleted row
reads back as null.
Updating a parent
A row’s update writes the values of its children, and of the rows a relation declares
[Property(Cascade = CascadeOptions.Update)] for (including self-referencing and mutually-referencing
[ForeignKey] relations), and no others. A plain list or reference of objects with a key only links them:
update those objects themselves, or declare Cascade = CascadeOptions.Update on the property.
Deleting a parent deletes its children
Children belong to their parent, so deleting the parent deletes them too:
var ops = db.Find<Department>(d => d.Name == "Ops");
db.Delete(ops); // its employees are deleted with it
This holds for every children collection, whether marked [Children] or matched by convention. A reference
by key ([ForeignKey]) isn’t ownership by default: deleting a book leaves its author alone. If a row should
outlive the one that points at it, model it as a reference, not a child.
- A
[Children]list goes with its parent regardless of itsCascadesetting. Deleting a parent keeps a child whose stored row names a different parent. - Objects and lists of objects without a key go with the row, with their
Deletetriggers. - A reference with
[Property(Cascade = CascadeOptions.Delete)](including self-referencing and mutually-referencing[ForeignKey]relations) deletes its target row when the holder is deleted or when an update clears the reference. - An
objectproperty deletes a keyed target row only when it declaresCascade = CascadeOptions.Delete. - Each row deleted by a cascade fires its own triggers, after its parent’s row is removed (see Events).
Deleting a child
Deleting a child on its own (db.Delete(employee)) removes its row and takes it out of its parent’s list
(including children that hold only their parent’s key by convention, with no parent reference property); the
parent stays. Moving a child by updating its parent key updates its parent’s list too.
Choosing what to load
Reads load every relationship by default. Every read method takes an include argument to load less:
// everything (the default)
var full = db.Find<Department>(d => d.Id == 1, include: "*");
// only the Employees collection
var withStaff = db.Find<Department>(d => d.Id == 1, include: nameof(Department.Employees));
// several: a comma-separated list of property names
var some = db.Find<Book>(b => b.Id == 1, include: "Author,Reviews");
A relationship you don’t include comes back null. Loading less is faster on large graphs.
Two options change the default for the whole database: DatabaseOptions.LazyLoad makes reads skip
relationships unless you include them, and LazyLoadParent (on by default) skips the parent reference
of a child unless you include it, so loading a child doesn’t load its whole family.