Skip to content

Commit

Permalink
docs: Update ManyToMany docs (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
meghanmae authored Aug 26, 2024
1 parent 177da1d commit d84bda8
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions docs/modeling/model-components/attributes/many-to-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ViewModel.
The named specified in the attribute will be used as the name of a collection of the objects on the other side of the relationship in the generated [TypeScript ViewModels](/stacks/disambiguation/view-model.md).

## Example Usage

In this example, we have a Person entity and an Appointment entity that share a many-to-many relationship. The PersonAppointment entity serves as the required middle table.
``` c#
public class Person
{
Expand All @@ -22,6 +22,26 @@ public class Person
[ManyToMany("Appointments")]
public ICollection<PersonAppointment> PersonAppointments { get; set; }
}

public class Appointment
{
public int AppointmentId { get; set; }
public DateTime AppointmentDate { get; set; }

[ManyToMany("People")]
public ICollection<PersonAppointment> PersonAppointments { get; set; }
}

public class PersonAppointment
{
public int PersonAppointmentId { get; set; }

public int PersonId { get; set; }
public Person Person { get; set; }

public int AppointmentId { get; set; }
public Appointment Appointment { get; set; }
}
```

## Properties
Expand All @@ -33,4 +53,32 @@ The name of the collection that will contain the set of objects on the other sid

<Prop def="public string FarNavigationProperty { get; set; }" />

The name of the navigation property on the middle entity that points at the far side of the many-to-many relationship. Use this to resolve ambiguities when the middle table of the many-to-many relationship has more than two reference navigation properties on it.
The name of the navigation property on the middle entity that points at the far side of the many-to-many relationship. Use this to resolve ambiguities when the middle table of the many-to-many relationship has more than two reference navigation properties on it.

``` c#
public class Person
{
...

[ManyToMany("Appointments", FarNavigationProperty = nameof(PersonAppointment.Appointment))]
public ICollection<PersonAppointment> PersonAppointments { get; set; }
}

public class Appointment
{
...

[ManyToMany("People", FarNavigationProperty = nameof(PersonAppointment.Person))]
public ICollection<PersonAppointment> PersonAppointments { get; set; }
}

public class PersonAppointment
{
...

// Adding a third reference navigation property in the middle table requires
// the use of FarNavigationProperty in order to resolve ambiguity.
public int WaiverId { get; set; }
public Waiver Waiver { get; set; }
}
```

0 comments on commit d84bda8

Please sign in to comment.