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:
Employee.TerritoryId (Singular - SingularId)
Employee.Territory_id (Singular - Singular_Id)
Employee.TerritoriesId (Singular - PluralId)
Employee.Territories_Id (Singular - Plural_Id)
Employee.TerritoryId (Singular - Child PK)
Employees.TerritoryId (Plural - SingularId)
Employees.Territory_id (Plural - Singular_Id)
Employees.TerritoriesId (Plural - PluralId)
Employees.Territories_Id (Plural - Plural_Id)
Employees.TerritoryId (Plural - Child PK)
Options
| ChildColumn | Defines 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;
}
|
| ParentColumn | Defines 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;
}
|
| ChildProperty | Defines 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;
}
|
| ParentProperty | Defines 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;
}
|
| SyncSource | SyncSource / 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. |
| SyncTarget | See above |