4. HasManyAndBelongsToMany

Defines a n-n relationship for a child object. In the example below, a Territory can have multiple employees and a Employee can have multiple territories.

Example:

[ElementaryClass]
public class Territory
{
  [Persist]
  public string TerritoryDescription;

  [Persist]
  public string TerritoryID;
        
  [HasManyAndBelongsToMany(typeof(Employee)]
  public List<Employee> Employees;
}

Remarks

A good indication of a n-n relationship is a join table. Elementary will automatically infer the join table and respective columns to use if not specified. Let's assume that this is our table structure:

Employee
--------------------
- EmployeeId
- FirstName
- LastName

EmployeeTerritory
--------------------
- Id
- EmployeeId
- TerritoryId

Territory
--------------------
- TerritoryId
- Description

If the join table isn't explicitly defined, Elementary will look for various matches using a combination of plural and singular patterns.

Options

Through tableDefines the through table (i.e. join table) to use
[ElementaryClass]
public class Territory
{
  [Persist]
  public string TerritoryDescription;

  [Persist]
  public string TerritoryID;
        
  [HasManyAndBelongsToMany(typeof(Employee), "employeeterritory")]
  public List<Employee> Employees;
}