Guides

Writing data

Insert one object or many, update all or part of an object, insert-or-update, delete by object or by condition, and drop a table.

With the default durability (Immediate), each write is in the database’s files when the call returns (see Durability). A write that fails leaves nothing behind in any durability mode or transaction, however many rows it touches. A process crash mid-write outside a transaction isn’t covered (see Writes outside a transaction). To group writes across a crash, use Transactions.

Insert

Customer nadia = db.Insert(new Customer { Name = "Nadia" });
Console.WriteLine(nadia.Id); // the assigned key

Insert<T> returns the object with its key filled in, and stores its relationships with it.

Many at once

InsertRange stores a batch in one write and returns how many rows it wrote. For bulk loads it is much faster than calling Insert in a loop.

int written = db.InsertRange(products);

An item refused (a handler throws, a key is already used) leaves none of the range written. In Buffered mode, InsertRange waits for Flush(). Wrap the call in a transaction when the batch must land whole across a crash too.

Update

Change the object and pass it back. XOD finds the stored row by its primary key:

var customer = db.Find<Customer>(c => c.Id == 1);
customer.Name = "Nadia K.";
db.Update(customer);

Update returns false if no row has that key. A non-autonumber key part at its type’s default throws MissingPrimaryKeyValueException (see Primary keys). Leaving an autonumber property unset (0) keeps the row’s stored number, and a number an update sets isn’t handed out again.

Only some properties

An UpdateFilter limits what is written. With Behavior = Target, only the named properties change and the rest keep their stored values; with Skip, everything except the named properties changes.

product.Name = "Kettle, steel";
product.Price = 99; // not written

db.Update(product, new UpdateFilter
{
    Properties = new[] { nameof(Product.Name) },
    Behavior = UpdateFilterBehavior.Target,
});

Properties the filter leaves out keep their stored values wherever they’re stored (elements or attributes), and unique keys and indexes check the row as stored. A filter takes a relation and its key property together, for both Target and Skip.

Replacing one object with another

Update(item, newItem) finds the stored row matching item and replaces it with newItem. Update and Delete match by example the same way a query by example does, with or without an index: text exactly and numbers by value.

Insert or update

InsertOrUpdate updates the row with the same key if there is one, and inserts otherwise:

db.InsertOrUpdate(new Product { Sku = "P2", Name = "Two (updated)" }); // P2 exists: updated
db.InsertOrUpdate(new Product { Sku = "P3", Name = "Three" });         // P3 is new: inserted

Delete

db.Delete(order);                                          // by object, matched on its key
int removed = db.DeleteWhere<Product>(p => p.Price < 3);   // by condition; returns the count

DeleteWhere is one write: a delete refused part way leaves every match intact. Its predicate runs inside the write, so other threads wait for it (don’t wait on them from inside the predicate) and writes made in it are part of the statement. It returns the number of rows deleted and fires one pair of triggers per row, equal rows included. In Buffered mode, Delete and DeleteWhere wait for Flush().

Deleting a parent deletes its children (see Relationships), and deleting a child on its own removes it from its parent’s list (see Deleting a child).

Drop a table

db.Drop<Product>();   // every Product row, and the table's files
db.DropAll();         // every table in the database

Drop and DropAll are atomic writes: a drop refused by a handler leaves every table intact.

Async

InsertAsync, UpdateAsync and DeleteAsync do the same work without blocking the calling thread and take a CancellationToken.

Writes from many threads

Any number of threads can write through one XodContext. Writes are applied one at a time, whatever their table: a write waits for reads under way, and reads wait for it.

Don’t write from inside a read on the same thread, such as from a query’s predicate or while enumerating results: every write and maintenance call there (BeginTransaction, Commit, Rollback, Flush, ClearCache, Drop, GetStatistics, password changes) throws XodException, and the context keeps working. A write on another thread waits for the read too, so a read that blocks on another thread’s write (InsertAsync(...).Wait() in a predicate) will deadlock. Writes inside a BeforeAction or AfterAction handler are allowed.

Source on GitHub · Report a problem with this page