Reference

Attributes

Every attribute you can put on a model property, what it takes and what it does.

All attributes are in the Xod namespace and go on properties. Derived classes inherit XOD attributes from the virtual base properties they override; an attribute on the override replaces the base property’s of the same kind.

Keys and indexes

Attribute What it does
[PrimaryKey] Makes the property the primary key. Put it on several properties for a composite key. Without it, a property named Id is the key. A non-autonumber key part can’t be its type’s default (see Primary keys).
[UniqueKey] Rejects duplicate values across the table (ReservedUniqueKeyException) and indexes the property.
[Property(Indexed = true)] Adds a secondary index.
[Property(AutoNumber = true)] XOD assigns increasing values on insert. Numbers and Guid only. IdentitySeed and IdentityIncrement set the first value and the step (see below).

Relationships

Attribute What it does
[Children] Marks a collection of objects this one owns; deleting the parent deletes them regardless of the list’s Cascade setting. Optional when each child has a property of the parent’s type or named <Parent>Id. RemoteParentProperty names the parent reference on the child when it can’t be found by type.
[ParentKey(localProperty)] On a child: the reference to its parent, and the property holding the parent’s key. [ParentKey(local, remote)] when the parent’s key isn’t its primary key.
[ForeignKey(localProperty)] A reference to another object by key, without ownership. [ForeignKey(local, remote)] names the remote key.
[Property(Cascade = ...)] Cascade options for a relationship. Children always cascade deletes and updates. CascadeOptions.Update (or All) makes a row’s update write the values of the referenced rows; without it, the update only links keyed rows. CascadeOptions.Delete deletes referenced rows with the row and when an update clears or drops them. Works on self-referencing and mutually-referencing [ForeignKey] relations too. See Updating a parent.

See Relationships.

Values

Attribute What it does
[Required] Rejects null or a default value (RequiredPropertyException).
[NotMapped] Not stored.
[Crypto(CryptoMethod.SHA256)] Stores a one-way hash. SHA512 also; MD5 and SHA1 for 1.x data.
[Crypto(CryptoMethod.AES256)] Stores the value encrypted with AES-256-GCM; reads return it decrypted. Requires a database password or DatabaseOptions.PropertyEncryptionKey (see A key of the database’s own).
[Markup] The value is HTML or XML; the XML format keeps it readable in a CDATA section.
[Property(Position = ValuePosition.Attribute)] XML format: write the value as an attribute of the row element.

[Crypto] applies to string properties only (elements or attributes), and can’t be combined with Indexed = true, [UniqueKey] or [PrimaryKey] (CryptoDataTypeException or IndexDataTypeException when the type is first used).

See Modelling data and Passwords and encryption.

Generic types

Attribute What it does
[GenericTypeProperty(name)] For a property of an open or base type: names the property that records the concrete full type name (e.g. System.Int32).
[Inherited] Obsolete (warning CS0618); it has no effect. Overrides inherit base attributes automatically.

The Property attribute in full

[Property] combines settings in one attribute:

[Property(Indexed = true, Position = ValuePosition.Body, AutoNumber = false)]

On an AutoNumber property, IdentitySeed and IdentityIncrement set where numbering starts and its step:

public class Invoice
{
    [PrimaryKey, Property(AutoNumber = true, IdentitySeed = 1000, IdentityIncrement = 5)]
    public int Number { get; set; }

    public string Customer { get; set; }
}

The first invoice gets 1000 and the next 1005. Both default to 1 and are typed as object. Each must be a whole number; the seed must be at least 1 and fit the property’s type, and the increment must be at least 1. A Guid autonumber accepts neither. Invalid settings throw AutonumberSeedException when the type is first used. A float or double autonumber throws OverflowException past 2^24 or 2^53 (the last exact whole numbers), just as an int stops at the end of its range.

Source on GitHub · Report a problem with this page