请帮助我弄清楚如何将表值函数公开为 Web Api 2 OData v4 服务中实体集的属性。
我的简化架构包含三个表,即 Structures、Locations 和 LocationLinks。一个结构包含一个带有节点(Locatons)和边(LocationLinks)的图。我使用 Entity Framework 6 Database 第一个模型进行访问。
Simplified Schema
Structure:
ID
Locations:
ID
ParentID -> Structure
LocationLinks
A -> Location
B -> Location
目标是访问 LocationLinks 的结构集合,就像我访问结构的位置一样。即请求结构#180的图表:
http://.../OData/Structures(180)/LocationLinks
http://.../OData/Structures(180)/Locations
Locations 查询自动运行,但我不知道如何添加正确的路线以启用 LocationLinks 查询。认为这会使我的任务更轻松,我在我的 SQL 服务器中添加了一个表值函数。该函数存在于我的 EF 模型中,并返回 LocationLink 实体的集合:
StructureLocationLinks(@StructureID) -> LocationLinks
不幸的是,无论我尝试什么,我似乎都无法使 Structure(180)\LocationLinks URL 正常工作。这是我最近的尝试:
StructuresController.cs 片段:
// GET: odata/Structures(5)/Locations
[EnableQuery]
public IQueryable<Location> GetLocations([FromODataUri] long key)
{
return db.Structures.Where(m => m.ID == key).SelectMany(m => m.Locations);
}
// GET: odata/Structures(5)/LocationLinks
[EnableQuery]
//[System.Web.OData.Routing.ODataRoute("Structures({key})")]
public IQueryable<LocationLink> GetLocationLinks([FromODataUri] long key)
{
return db.StructureLocationLinks(key);
}
WebApi.cs 片段:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer = true;
//json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
//var cors = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*");
//config.EnableCors(cors);
// Web API routes
config.MapHttpAttributeRoutes();
ODataConventionModelBuilder builder = GetModel();
config.MapODataServiceRoute(routeName: "odata",
routePrefix: null,
model: builder.GetEdmModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public static ODataConventionModelBuilder GetModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = typeof(Structure).Namespace;
AddLocationLinks(builder);
AddStructures(builder);
builder.EntitySet<Location>("Locations");
return builder;
}
public static void AddStructures(ODataModelBuilder builder)
{
var structSetconfig = builder.EntitySet<Structure>("Structures");
var structConfig = structSetconfig.EntityType;
var functionConfig = structConfig.Collection.Function("StructureLocationLinks");
functionConfig.Parameter<long>("StructureID");
functionConfig.Returns<LocationLink>();
}
public static void AddLocationLinks(ODataModelBuilder builder)
{
var type = builder.EntityType<LocationLink>();
type.HasKey(sl => sl.A);
type.HasKey(sl => sl.B);
builder.EntitySet<LocationLink>("LocationLinks");
}
我收到的错误是:
{ "error":{ "code":"","message":"没有找到与请求 URI ' http://.../OData/Structures(180)/LocationLinks '匹配的 HTTP 资源。"," innererror":{ "message":"没有找到路由约定来为带有模板 '~/entityset/key/unresolved' 的 OData 路径选择一个操作。","type":"","stacktrace":"" } } }
基于一些搜索,我尝试向控制器添加 ODataRoute 属性:
// GET: odata/Structures(5)/LocationLinks
[EnableQuery]
[System.Web.OData.Routing.ODataRoute("Structures({key})/LocationLinks")]
public IQueryable<LocationLink> GetLocationLinks([FromODataUri] long key)
{
return db.StructureLocationLinks(key);
}
导致此错误:
控制器“结构”中操作“GetLocationLinks”的路径模板“结构({key})/LocationLinks”不是有效的 OData 路径模板。在 OData 路径模板“结构({key})/LocationLinks”中找到未解析的路径段“LocationLinks”。
如何从 Structures 集合中公开 LocationLinks?感谢您的时间。
编辑:
找到这个问题后,我设法让它工作: Adding a custom query backed Navigation Property to ODataConventionModelBuilder
我必须在我的 ODataConventionBuilder 对象上调用 .GetEdmModel,然后使用此函数将导航属性添加到模型中:
private static Microsoft.OData.Edm.IEdmModel AddStructureLocationLinks(IEdmModel edmModel)
{
var structures = edmModel.EntityContainer.FindEntitySet("Structures") as EdmEntitySet;
var locationLinks = edmModel.EntityContainer.FindEntitySet("LocationLinks") as EdmEntitySet;
var structType = structures.EntityType() as EdmEntityType;
var locLinksType = locationLinks.EntityType() as EdmEntityType;
var structLocLinksProperty = new EdmNavigationPropertyInfo();
structLocLinksProperty.TargetMultiplicity = Microsoft.OData.Edm.EdmMultiplicity.Many;
structLocLinksProperty.Target = locLinksType;
structLocLinksProperty.ContainsTarget = true;
structLocLinksProperty.OnDelete = Microsoft.OData.Edm.EdmOnDeleteAction.None;
structLocLinksProperty.Name = "LocationLinks";
var navigationProperty = structType.AddUnidirectionalNavigation(structLocLinksProperty);
structures.AddNavigationTarget(navigationProperty, locationLinks);
return edmModel;
}
我现在遇到的问题是我在查询中访问导航属性的能力有限。例如此链接有效:
http://.../OData/Structures(180)/Children?$expand=Locations
虽然这没有。
http://.../OData/Structures(180)/Children?$expand=LocationLinks
返回的错误是
{ "error": { "code":"","message":"发生错误。","innererror": { "message":"实例属性 'LocationLinks' 没有为类型 'ConnectomeDataModel.Structure' 定义", "type":"System.ArgumentException","stacktrace":" at System.Linq.Expressions.Expression.Property(Expression expression, String propertyName)\r\n at System.Web.OData.Query.Expressions.SelectExpandBinder .CreatePropertyValueExpressionWithFilter(IEdmEntityType elementType, IEdmProperty property, Expression source, FilterClause filterClause)\r\n at System.Web.OData.Query.Expressions.SelectExpandBinder.BuildPropertyContainer(IEdmEntityType elementType, Expression source,字典
2 propertiesToExpand, ISet
1 propertiesToInclude, ISet1 autoSelectedProperties, Boolean isSelectingOpenTypeSegments)\r\n at System.Web.OData.Query.Expressions.SelectExpandBinder.ProjectElement(Expression source, SelectExpandClause selectExpandClause, IEdmEntityType entityType)\r\n at System.Web.OData.Query.Expressions.SelectExpandBinder.Bind(IQueryable queryable)\r\n at System.Web.OData.Query.ODataQueryOptions.ApplySelectExpand[T](T entity, ODataQuerySettings querySettings)\r\n at System.Web.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)\r\n at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)\r\n at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- 从先前引发异常的位置结束堆栈跟踪 ---\r\n在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() \r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" } } }