Skip to content

Commit

Permalink
add an officially supported method to get all navigation property bin…
Browse files Browse the repository at this point in the history
…dings
  • Loading branch information
chrisspre committed Aug 12, 2021
1 parent 6edadf5 commit 419e150
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,39 @@ public static IEnumerable<IEdmOperationImport> OperationImports(this IEdmEntityC
return container.AllElements().OfType<IEdmOperationImport>();
}

/// <summary>
/// Returns NavigationPropertyBindings with their corresponding container elements (EntitySet or Singelton)
/// </summary>
/// <param name="container">Reference to the calling object.</param>
/// <returns>collection of pairs of container element and NavigationPropertyBindings.</returns>
public static IEnumerable<(IEdmEntityContainerElement, IEdmNavigationPropertyBinding)> GetNavigationPropertyBindings(this IEdmEntityContainer container)
{
EdmUtil.CheckArgumentNull(container, "container");

var elements = container.AllElements();
foreach(var element in elements)
{
switch (element)
{
case IEdmEntitySet entitySet:
foreach(var binding in entitySet.NavigationPropertyBindings)
{
yield return (entitySet, binding);
}
break;
case IEdmSingleton singleton:
foreach (var binding in singleton.NavigationPropertyBindings)
{
yield return (singleton, binding);
}
break;
default:
break;
}
}
}


#endregion

#region IEdmTypeReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,28 @@ public void EnsureActionImportActionPropertyIsUnresolvedAction()
Assert.Equal(EdmContainerElementKind.ActionImport, imports[0].ContainerElementKind);
Assert.Null(imports[0].EntitySet);
}

[Fact]
public void NavigationPropertyBindingsReturned()
{
// arrange
var entitySet1 = new CsdlEntitySet("EntitySet1", "unknown", new[] {
new CsdlNavigationPropertyBinding("foo", "bar", testLocation)
}, testLocation);
var singleton1 = new CsdlSingleton("Singleton", "unknown", new[] {
new CsdlNavigationPropertyBinding("foo", "bar", testLocation)
}, testLocation);
var csdlEntityContainer = CsdlBuilder.EntityContainer("Container", entitySets: new [] { entitySet1 }, singletons: new[] { singleton1 } );
var schema = CsdlBuilder.Schema("FQ.NS", csdlEntityContainers: new CsdlEntityContainer[] { csdlEntityContainer });
var csdlModel = new CsdlModel();
csdlModel.AddSchema(schema);

// act
var container = (IEdmEntityContainer)csdlEntityContainer;
var bindings = container.GetNavigationPropertyBindings().ToList();

// assert
Assert.Equal(2, bindings.Count);
}
}
}

0 comments on commit 419e150

Please sign in to comment.