Skip to content

Foreign Keys

Aaron Aichlmayr edited this page Apr 11, 2019 · 1 revision

Foreign keys are represented by a number specifically annotated properties on both sides of the relationsip

First steps for all relationships

Identify or create properties on both sides of the relationship which represent the fields used in the foreign key relationship. These will need to be annotated using SqlField annotations.

One-To-One / One-To-Many

  1. Create a property on both sides of the relationship to represent the object linkage
    • If on the one side of the relationship or it is One-To-One it should be public ForeignObject propertyName { get; set; }
    • If on the many side of the relationship it should be public IEnumerable<ForeignObject> propertyName { get; set; }
  2. Add a [SqlGraphQLRelated(LocalProperty = "", ForeignProperty = "")] annotation to both new properties. The LocalProperty and ForeignProperty values should contain the property names (not field names) identified in the first steps section.

Note: The two properties should contain the same values swapping Foreign and Local values

Many-To-Many

  1. Create a property on both sides of the relationship to represent the object linkage public IEnumerable<ForeignObject> propertyName { get; set; }
  2. Add a [SqlGraphQLRelated( LocalProperty = "", LocalJoinField = "", JoinTable = "", ForeignJoinField = "", ForeignProperty = "" )] annotation to each property.
    1. The LocalProperty and ForeignProperty values should contain the property names (not field names) identified in the first steps section.
    2. The JoinTable should contain the table used for the relationship
    3. The LocalJoinField should contain the database field in the JoinTable which matches the LocalProperty
    4. The ForeignJoinField should contain the database field in the JoinTable which matches the ForeignProperty

Note: The two properties should contain the same values swapping Foreign and Local values