From 39ea216dd57b1e43c9ac377dd774f54039db1f10 Mon Sep 17 00:00:00 2001 From: Meghan Date: Sat, 17 Aug 2024 14:02:55 -0700 Subject: [PATCH 1/2] Update ManyToMany docs --- .../attributes/many-to-many.md | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/docs/modeling/model-components/attributes/many-to-many.md b/docs/modeling/model-components/attributes/many-to-many.md index 397ae458b..2236a6a8d 100644 --- a/docs/modeling/model-components/attributes/many-to-many.md +++ b/docs/modeling/model-components/attributes/many-to-many.md @@ -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 { @@ -22,6 +22,26 @@ public class Person [ManyToMany("Appointments")] public ICollection PersonAppointments { get; set; } } + +public class Appointment +{ + public int AppointmentId { get; set; } + public DateTime AppointmentDate { get; set; } + + [ManyToMany("People")] + public ICollection 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 @@ -33,4 +53,40 @@ The name of the collection that will contain the set of objects on the other sid -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. \ No newline at end of file +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 +{ + public int PersonId { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + + [ManyToMany("Appointments", FarNavigationProperty = nameof(PersonAppointment.Appointment))] + public ICollection PersonAppointments { get; set; } +} + +public class Appointment +{ + public int AppointmentId { get; set; } + public DateTime AppointmentDate { get; set; } + + [ManyToMany("People", FarNavigationProperty = nameof(PersonAppointment.Person))] + public ICollection 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; } + + // Additional reference requiring the use of FarNavigationProperty + public int WaiverId { get; set; } + public Waiver Waiver { get; set; } +} +``` \ No newline at end of file From 3980882f06b65225c66394625b1d1623c82347ca Mon Sep 17 00:00:00 2001 From: Meghan <43832670+meghanmae@users.noreply.github.com> Date: Sun, 25 Aug 2024 11:41:31 -0700 Subject: [PATCH 2/2] Update docs/modeling/model-components/attributes/many-to-many.md Co-authored-by: Andrew Scott --- .../attributes/many-to-many.md | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/docs/modeling/model-components/attributes/many-to-many.md b/docs/modeling/model-components/attributes/many-to-many.md index 2236a6a8d..6adbc2868 100644 --- a/docs/modeling/model-components/attributes/many-to-many.md +++ b/docs/modeling/model-components/attributes/many-to-many.md @@ -58,18 +58,15 @@ The name of the navigation property on the middle entity that points at the far ``` c# public class Person { - public int PersonId { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } - + ... + [ManyToMany("Appointments", FarNavigationProperty = nameof(PersonAppointment.Appointment))] public ICollection PersonAppointments { get; set; } } public class Appointment { - public int AppointmentId { get; set; } - public DateTime AppointmentDate { get; set; } + ... [ManyToMany("People", FarNavigationProperty = nameof(PersonAppointment.Person))] public ICollection PersonAppointments { get; set; } @@ -77,15 +74,10 @@ public class Appointment 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; } + ... - // Additional reference requiring the use of FarNavigationProperty + // 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; } }