ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Core.Data Namespace / RelationshipClass Class / CreateRelationship Method
The Row from the origin table.
The Row from the destination table.
Example Version

CreateRelationship Method (RelationshipClass)
Creates a new relationship between the two specified rows. This method must be called on the MCT. Use QueuedTask.Run.
Syntax
public Relationship CreateRelationship( 
   Row originRow,
   Row destinationRow
)

Parameters

originRow
The Row from the origin table.
destinationRow
The Row from the destination table.

Return Value

A Relationship representing the pair of related rows.
Exceptions
ExceptionDescription
originRow or destinationRow is null.
This relationship class does not support this operation. For example, it is a virtual relationship class.
A geodatabase-related exception has occurred.
Remarks
For relationships of type RelationshipCardinality.OneToOne and RelationshipCardinality.OneToMany that do not have user-attributes defined, no intermediate table will be created to store relationships between the rows with corresponding origin foreign and destination foreign key values. Instead, the origin row's primary key value will be written into the destination row's origin foreign key field.
Example
Creating a Relationship
public async Task CreatingARelationship()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
    {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
        using (RelationshipClass relationshipClass = geodatabase.OpenDataset<RelationshipClass>("LocalGovernment.GDB.OverviewToProject"))
        using (FeatureClass projectsFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.CIPProjects"))
        using (FeatureClass overviewFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.CIPProjectsOverview"))
        {
            // This will be PROJNAME. This can be used to get the field index or used directly as the field name.
            string originKeyField = relationshipClass.GetDefinition().GetOriginKeyField();

            EditOperation editOperation = new EditOperation();
            editOperation.Callback(context =>
      {
          // The rows are being added to illustrate adding relationships. If one has existing rows, those can be used to add a relationship.
          using (RowBuffer projectsRowBuffer = projectsFeatureClass.CreateRowBuffer())
          using (RowBuffer overviewRowBuffer = overviewFeatureClass.CreateRowBuffer())
          {
              projectsRowBuffer["TOTCOST"] = 500000;

              overviewRowBuffer[originKeyField] = "LibraryConstruction";
              overviewRowBuffer["PROJECTMAN"] = "John Doe";
              overviewRowBuffer["FUNDSOUR"] = "Public";

              using (Row projectsRow = projectsFeatureClass.CreateRow(projectsRowBuffer))
              using (Row overviewRow = overviewFeatureClass.CreateRow(overviewRowBuffer))
              {
                  Relationship relationship = relationshipClass.CreateRelationship(overviewRow, projectsRow);

                  //To Indicate that the Map has to draw this feature/row and/or the attribute table has to be updated
                  context.Invalidate(projectsRow);
                  context.Invalidate(overviewRow);
                  context.Invalidate(relationshipClass);
              }
          }
      }, projectsFeatureClass, overviewFeatureClass);

            bool editResult = editOperation.Execute();
        }
    });
}
Requirements

Target Platforms: Windows 11, Windows 10

ArcGIS Pro version: 3 or higher.
See Also