5. HasOne

Defines a 1-1 relationship for a child object.

Example:

[ElementaryClass]
public class Employee
{
  [Persist]
  public string FirstName;

  [Persist]
  public string LastName;

  [HasOne]
  public Territory Territory;
}

Remarks

Elementary will automatically infer the parent and child columns to use. Let's assume that this is our table structure:

Employees
--------------------
- EmployeeId
- FirstName
- LastName
- TerritoryId

Territories
--------------------
- TerritoryId
- TerritoryName

If the columns aren't explicitly defined, Elementary will look for the following matches:

Options

ChildColumnDefines the column to use in the child table
[ElementaryClass]
public class Employee
{
  [Persist]
  public string FirstName;

  [Persist]
  public string LastName;

  [HasOne(ParentColumn="territoryid", ChildColumn="id")]
  public Territory Territory;
}
ParentColumnDefines the column to use in the parent table
[ElementaryClass]
public class Employee
{
  [Persist]
  public string FirstName;

  [Persist]
  public string LastName;

  [HasOne(ParentColumn="territoryid", ChildColumn="id")]
  public Territory Territory;
}
ChildPropertyDefines the property to use in the child table. This is translated behind the scenes into the corresponding column
[ElementaryClass]
public class Employee
{
  [Persist]
  public string FirstName;

  [Persist]
  public string LastName;

  [Persist]
  public int TerritoryId;

  [HasOne(ParentProperty="TerritoryId", ChildProperty="Id")]
  public Territory Territory;
}
ParentPropertyDefines the property to use in the parent table. This is translated behind the scenes into the corresponding column
[ElementaryClass]
public class Employee
{
  [Persist]
  public string FirstName;

  [Persist]
  public string LastName;

  [Persist]
  public int TerritoryId;

  [HasOne(ParentProperty="TerritoryId", ChildProperty="Id")]
  public Territory Territory;
}
SyncSourceSyncSource / SyncTarget allow you to sync the value of a child object with the parent object. This is most useful when you save an object and need to set the id of the child object in the parent.
[ElementaryClass]
public class Product
{
  [Persist]
  public int CategoryID = 0;
  [Persist]
  public int ProductID = 0;
  [Persist]
  public string ProductName = "";
  [Persist]
  public int SupplierID = 0;
  
  [HasOne(SyncSource = "Id", SyncTarget = "CategoryID")]
  public Category Category = null;
}

In the example above, the Products table has a column for CategoryId. By using SyncSource / SyncTarget, when we save a Product object, it'll grab Product.Category.Id and set Product.CategoryID with its value.

SyncSource is the child source property. SyncTarget is the parent target property.

SyncTargetSee above