Ef core 2.1 DbContext.Query上的EF Core 2.1 InvalidCastException<;T>;()
鉴于: 我在自己的实体中遇到了这个问题,并使用罐装的Ef core 2.1 DbContext.Query上的EF Core 2.1 InvalidCastException<;T>;(),ef-core-2.1,Ef Core 2.1,鉴于: 我在自己的实体中遇到了这个问题,并使用罐装的ApplicationDbContext/IdentityUser用最少的代码复制了它。这是.NETCore2.1中的一个bug还是我做了一些不正确的事情 这是DbContext.Query()的源代码: 公共虚拟数据库查询() 提问:课堂 =>(DbQuery)((IDbQueryCache)this) .GetOradQuery(DbContextDependencies.QuerySource,typeof(TQuery)); 它似乎正
ApplicationDbContext
/IdentityUser
用最少的代码复制了它。这是.NETCore2.1中的一个bug还是我做了一些不正确的事情
这是DbContext.Query()
的源代码:
公共虚拟数据库查询()
提问:课堂
=>(DbQuery)((IDbQueryCache)this)
.GetOradQuery(DbContextDependencies.QuerySource,typeof(TQuery));
它似乎正在将异常强制转换为((IDbQueryCache)this)。GetOradQuery(DbContextDependencies.QuerySource,typeof(TQuery))
到(DbQuery)
我已经在github上打开了一个应用程序,因为从我的判断,这似乎是一个bug。对我发布的github的评论有以下解决方案: 不能为EntityTypes调用
Query
方法。您必须使用Set
方法
如果可能的话,TQuery
比class
更受约束可能会有所帮助
public class MyClass
{
public MyClass(ApplicationDbContext db)
{
var query = db.Query<IdentityUser>();
}
}
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.EntityFrameworkCore.Internal.InternalDbSet\`1[Microsoft.AspNetCore.Identity.IdentityUser]' to type 'Microsoft.EntityFrameworkCore.DbQuery\`1[Microsoft.AspNetCore.Identity.IdentityUser]'.'
public virtual DbQuery<TQuery> Query<TQuery>()
where TQuery : class
=> (DbQuery<TQuery>)((IDbQueryCache)this)
.GetOrAddQuery(DbContextDependencies.QuerySource, typeof(TQuery));