.NET Core - EntityFrameworkCore - 无法将“Query.Internal.EntityQueryable”类型的对象转换为“DbSet”类型
.NET Core - EntityFrameworkCore - Unable to cast object of type 'Query.Internal.EntityQueryable` to type 'DbSet`
我尝试在提供搜索字段时使用实体进行搜索
但我遇到了一个奇怪的转换错误,我就是不明白
Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[SomeApp.Models.Partner]' to type 'Microsoft.EntityFrameworkCore.DbSet`1[SomeApp.Models.Partner]'.
这是我的控制器入口点的代码
我尝试强制转换,但显然我的代码有问题
[HttpPost]
public async Task<ActionResult<PartnersFetch>> GetPartners(PartnersSearch partnersSearch)
{
DbSet<Partner> data = _context.Partners;
if (partnersSearch.SearchById != null)
{
// the following line causes problems :
data = (DbSet <Partner>) data.Where( p => p.Id == partnersSearch.SearchById.GetValueOrDefault());
}
感谢你在这方面帮助我
data.Where(...)
将 return 一个 IQueryable,您可以按如下方式具体化
List<Partner> myResult = data.Where(...).ToList();
DbSet<Partner>
只是您可以查询数据的集合。您的目标很可能是让合作伙伴摆脱它,对吗?
我忘了使用 AsQueryable
var data = _context.Partners.AsQueryable();
if (partnersSearch.SearchById != null)
{
data = data.Where( p => p.Id == partnersSearch.SearchById.GetValueOrDefault());
}
我尝试在提供搜索字段时使用实体进行搜索
但我遇到了一个奇怪的转换错误,我就是不明白
Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[SomeApp.Models.Partner]' to type 'Microsoft.EntityFrameworkCore.DbSet`1[SomeApp.Models.Partner]'.
这是我的控制器入口点的代码
我尝试强制转换,但显然我的代码有问题
[HttpPost]
public async Task<ActionResult<PartnersFetch>> GetPartners(PartnersSearch partnersSearch)
{
DbSet<Partner> data = _context.Partners;
if (partnersSearch.SearchById != null)
{
// the following line causes problems :
data = (DbSet <Partner>) data.Where( p => p.Id == partnersSearch.SearchById.GetValueOrDefault());
}
感谢你在这方面帮助我
data.Where(...)
将 return 一个 IQueryable,您可以按如下方式具体化
List<Partner> myResult = data.Where(...).ToList();
DbSet<Partner>
只是您可以查询数据的集合。您的目标很可能是让合作伙伴摆脱它,对吗?
我忘了使用 AsQueryable
var data = _context.Partners.AsQueryable();
if (partnersSearch.SearchById != null)
{
data = data.Where( p => p.Id == partnersSearch.SearchById.GetValueOrDefault());
}