diff --git a/AutoMapper.AspNetCore.OData.EFCore/Expansions/DefaultExpansionsBuilder.cs b/AutoMapper.AspNetCore.OData.EFCore/Expansions/DefaultExpansionsBuilder.cs index 988047d..22f6467 100644 --- a/AutoMapper.AspNetCore.OData.EFCore/Expansions/DefaultExpansionsBuilder.cs +++ b/AutoMapper.AspNetCore.OData.EFCore/Expansions/DefaultExpansionsBuilder.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.OData.Query; using Microsoft.OData.Edm; +using Microsoft.OData.ModelBuilder; using System; using System.Collections.Generic; using System.Globalization; @@ -125,12 +126,12 @@ private HashSet GetNavigationProperties(Type type) IEdmEntityType entityType = context.Model.SchemaElements.OfType() .SingleOrDefault(e => GetClrTypeFromEntityType(e).FullName == type.FullName); if (entityType != null) - return entityType.NavigationProperties().Select(p => p.Name).ToHashSet(); + return entityType.NavigationProperties().Select(GetNavigationPropertyName).ToHashSet(); IEdmComplexType complexType = context.Model.SchemaElements.OfType() .SingleOrDefault(e => GetClrTypeFromComplexType(e).FullName == type.FullName); if (complexType != null) - return complexType.NavigationProperties().Select(p => p.Name).ToHashSet(); + return complexType.NavigationProperties().Select(GetNavigationPropertyName).ToHashSet(); return []; @@ -139,6 +140,14 @@ Type GetClrTypeFromEntityType(IEdmEntityType entityType) Type GetClrTypeFromComplexType(IEdmComplexType complexType) => TypeExtensions.GetClrType(new EdmComplexTypeReference(complexType, true), context.Model, TypeExtensions.GetEdmToClrTypeMappings()); + + // Look up the name of the corresponding C# property, which may differ from the property name + // used in the EDM model, e.g. because the EnableLowerCamelCase option is being used. + string GetNavigationPropertyName(IEdmNavigationProperty prop) + { + var annotation = context.Model.GetAnnotationValue(prop); + return annotation?.ClrPropertyInfo?.Name ?? prop.Name; + } } } } diff --git a/AutoMapper.OData.EF6.Tests/GetTests.cs b/AutoMapper.OData.EF6.Tests/GetTests.cs index 13171d8..551d07a 100644 --- a/AutoMapper.OData.EF6.Tests/GetTests.cs +++ b/AutoMapper.OData.EF6.Tests/GetTests.cs @@ -645,6 +645,8 @@ public static ODataQueryOptions GetODataQueryOptions(string queryString, I if (customNamespace != null) builder.Namespace = customNamespace; + builder.EnableLowerCamelCase(); + builder.EntitySet(typeof(T).Name); IEdmModel model = builder.GetEdmModel(); IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet(typeof(T).Name); diff --git a/AutoMapper.OData.EFCore.Tests/GetTests.cs b/AutoMapper.OData.EFCore.Tests/GetTests.cs index 23964d5..15067a2 100644 --- a/AutoMapper.OData.EFCore.Tests/GetTests.cs +++ b/AutoMapper.OData.EFCore.Tests/GetTests.cs @@ -686,6 +686,8 @@ public static ODataQueryOptions GetODataQueryOptions(string queryString, I if (customNamespace != null) builder.Namespace = customNamespace; + builder.EnableLowerCamelCase(); + builder.EntitySet(typeof(T).Name); IEdmModel model = builder.GetEdmModel(); IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet(typeof(T).Name); @@ -706,6 +708,8 @@ public static ODataQueryOptions GetODataQueryOptions(string queryString, I if (customNamespace != null) builder.Namespace = customNamespace; + builder.EnableLowerCamelCase(); + builder.EntitySet(typeof(X.CategoryModel).Name + "X"); builder.EntitySet(nameof(Model.CategoryModel)); IEdmModel model = builder.GetEdmModel(); diff --git a/ExpressionBuilder.Tests/ODataHelpers.cs b/ExpressionBuilder.Tests/ODataHelpers.cs index dd3dcc7..9c20d0f 100644 --- a/ExpressionBuilder.Tests/ODataHelpers.cs +++ b/ExpressionBuilder.Tests/ODataHelpers.cs @@ -25,6 +25,7 @@ private static IEdmModel GetModel() where T : class IEdmModel GetModel(ODataConventionModelBuilder builder) { + builder.EnableLowerCamelCase(); builder.EntitySet(modelType.Name); if (modelType == typeof(Product)) { @@ -51,6 +52,7 @@ public static SelectExpandClause GetSelectExpandClause(IDictionary(IDictionary queryO entitySet, queryOptions ); + parser.Resolver = new ODataUriResolver { EnableCaseInsensitive = true }; if (useFilterOption) { @@ -108,6 +111,7 @@ public static ODataQueryContext GetODataQueryContext() where T : class public static ODataQueryOptions GetODataQueryOptions(string queryString, IServiceProvider serviceProvider, IRouteBuilder routeBuilder) where T : class { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); + builder.EnableLowerCamelCase(); builder.EntitySet(typeof(T).Name); IEdmModel model = builder.GetEdmModel(); diff --git a/WebAPI.OData.EF6/Startup.cs b/WebAPI.OData.EF6/Startup.cs index 4a30463..f48b8e3 100644 --- a/WebAPI.OData.EF6/Startup.cs +++ b/WebAPI.OData.EF6/Startup.cs @@ -71,6 +71,7 @@ private IEdmModel GetEdmModel() { var builder = new ODataConventionModelBuilder(); //builder.Namespace = "com.FooBar"; + builder.EnableLowerCamelCase(); builder.EntitySet(nameof(OpsTenant)); builder.EntitySet(nameof(CoreBuilding)); builder.EntitySet(nameof(OpsBuilder)); diff --git a/WebAPI.OData.EFCore/Startup.cs b/WebAPI.OData.EFCore/Startup.cs index 3c6622c..efe8cfd 100644 --- a/WebAPI.OData.EFCore/Startup.cs +++ b/WebAPI.OData.EFCore/Startup.cs @@ -66,6 +66,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) private IEdmModel GetEdmModel() { var builder = new ODataConventionModelBuilder(); + builder.EnableLowerCamelCase(); //builder.Namespace = "com.FooBar"; builder.EntitySet(nameof(OpsTenant)); builder.EntitySet(nameof(CoreBuilding));