【发布时间】:2019-09-02 07:21:00
【问题描述】:
我正在尝试通过 EF Core 和 OData(7.1.0) 实现对 Sql Server 的查询。
动作方法如下:
[HttpGet]
public IEnumerable<UserInfoDto> Get(ODataQueryOptions ops)
{
return this.service.GetUserInfos(ops);
}
服务代码:
public List<UserInfoDto> GetUserInfos(ODataQueryOptions ops)
{
using (var context = new EFContext())
{
var query = context.Users.Join(context.Customers, x => x.CustomerId, y => y.Id, (x, y) => new UserInfoDto
{
Id = x.Id,
Name = x.Name,
Age = x.Age,
CustomerId = x.CustomerId,
CustomerTitle = y.Title,
CustomerDescription = y.Description
});
var result = ops.ApplyTo(query).Cast<UserInfoDto>().ToList();
return result;
}
}
启动Configute方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc(b =>
{
b.Count().Filter().OrderBy().Select().MaxTop(null);
b.EnableDependencyInjection();
});
}
但是,当我在查询中使用 $select(例如 https://localhost:5001/api/userinfos?$select=id)时,我收到的不是预测结果,而是一个错误:
InvalidOperationException:类型之间没有定义强制运算符 'Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder+SelectSome`1[小田 taApp.UserInfoDto]' 和 'OdataApp.UserInfoDto'。
我错过了什么?任何帮助表示赞赏。
【问题讨论】:
标签: c# asp.net-core entity-framework-core odata