我正在尝试用Automapper投射我的EF核心查询,以允许我的应用程序中的第三层,但我有一些困难,允许HotChocolate请求我的DTO上的字段,并告诉Automapper在投影过程中包含这些字段。
事先有几个要点:
如果地图上设置了
ExplicitExpansion设置为IQueryable的导航属性所以我可以一次加载所有导航属性,或者不加载。
如何告诉以太HotChocolate映射IQueryable中的实体,或者如何在查询函数中获得所需的键,以通过使用IQueryable<T>.ProjectTo()方法告诉AutoMapper要展开哪些属性?
发布于 2021-04-27 13:54:50
你试过吗?
public class Query
{
[UseProjection] //<----
public IQueryable<FooDto> GetFoos([Service]YourService svc)=> svc.GetFooDtos();
}如果投影不是太复杂,这应该是可行的。
如果投影的顺序有问题,也可以创建自定义属性。
public class YourCustomMiddlewareAttribute : ObjectFieldDescriptorAttribute
{
public override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
{
descriptor.Type<ListType<ObjectType<PersonDto>>>();
descriptor.Use(next => async context =>
{
await next(context);
if (context.Result is IQueryable<Person> persons)
{
context.Result = persons
.ProjectTo<PersonDto>()
.ToListAsync(context.RequestAborted);
}
})
}
}public class Query
{
[YourCustomMiddleware]
[UseProjection]
public IQueryable<FooDto> GetFoos([Service]YourService svc)=> svc.GetFooDtos();
}https://stackoverflow.com/questions/67284037
复制相似问题