Entity framework Include(),其中where子句失败

Entity framework Include(),其中where子句失败,entity-framework,linq,entity-framework-core,Entity Framework,Linq,Entity Framework Core,这很奇怪,有人能解释一下我为什么这样做很好: _db.Tasks.Include(t => t.Category).ThenInclude(c => c.ApplicationUser) .ToList(); 虽然这会引发异常: _db.Tasks.Include(t=>t.Category).ThenInclude(c=>c.ApplicationUser) .Where(t => t.Catego

这很奇怪,有人能解释一下我为什么这样做很好:

_db.Tasks.Include(t => t.Category).ThenInclude(c => c.ApplicationUser)
                .ToList();
虽然这会引发异常:

_db.Tasks.Include(t=>t.Category).ThenInclude(c=>c.ApplicationUser)
                .Where(t => t.Category.ApplicationUser.UserName == username)
                .ToList();
   at lambda_method(Closure , InternalEntityEntry )
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimpleNonNullableDependentKeyValueFactory`1.TryCreateFromCurrentValues(InternalEntityEntry entry, TKey& key)
   at Microsoft.EntityFrameworkCore.Query.Internal.WeakReferenceIdentityMap`1.CreateIncludeKeyComparer(INavigation navigation, InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCore(Object entity, INavigation navigation)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.Include(QueryContext queryContext, Object entity, IReadOnlyList`1 navigationPath, IReadOnlyList`1 relatedEntitiesLoaders, Int32 currentNavigationIndex, Boolean queryStateManager)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.Include(QueryContext queryContext, Object entity, IReadOnlyList`1 navigationPath, IReadOnlyList`1 relatedEntitiesLoaders, Boolean queryStateManager)
   at Microsoft.EntityFrameworkCore.Query.Internal.GroupJoinInclude.Include(Object entity)
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_GroupJoin>d__26`4.MoveNext()
   at System.Linq.Enumerable.<SelectManyIterator>d__163`3.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at _:line 56
由于某些原因,.Include()不适用于.Where子句。有什么想法吗

例外情况:

_db.Tasks.Include(t=>t.Category).ThenInclude(c=>c.ApplicationUser)
                .Where(t => t.Category.ApplicationUser.UserName == username)
                .ToList();
   at lambda_method(Closure , InternalEntityEntry )
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimpleNonNullableDependentKeyValueFactory`1.TryCreateFromCurrentValues(InternalEntityEntry entry, TKey& key)
   at Microsoft.EntityFrameworkCore.Query.Internal.WeakReferenceIdentityMap`1.CreateIncludeKeyComparer(INavigation navigation, InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCore(Object entity, INavigation navigation)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.Include(QueryContext queryContext, Object entity, IReadOnlyList`1 navigationPath, IReadOnlyList`1 relatedEntitiesLoaders, Int32 currentNavigationIndex, Boolean queryStateManager)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.Include(QueryContext queryContext, Object entity, IReadOnlyList`1 navigationPath, IReadOnlyList`1 relatedEntitiesLoaders, Boolean queryStateManager)
   at Microsoft.EntityFrameworkCore.Query.Internal.GroupJoinInclude.Include(Object entity)
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_GroupJoin>d__26`4.MoveNext()
   at System.Linq.Enumerable.<SelectManyIterator>d__163`3.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at _:line 56
型号:

public class Task
{
    [Key]
    public int TaskId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime Timestamp { get; set; }
    public int CategoryId { get; set; }

    public virtual Category Category { get; set; }
}

public class Category
{
    [Key]
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime Timestamp { get; set; }

    public string UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }

    public virtual ICollection<Task> Tasks { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<Category> Categories { get; set; }
}
公共类任务
{
[关键]
public int TaskId{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共日期时间时间戳{get;set;}
public int CategoryId{get;set;}
公共虚拟类别{get;set;}
}
公共类类别
{
[关键]
public int CategoryId{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共日期时间时间戳{get;set;}
公共字符串用户标识{get;set;}
[外键(“用户ID”)]
公共虚拟应用程序用户应用程序用户{get;set;}
公共虚拟ICollection任务{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共虚拟ICollection类别{get;set;}
}

在浪费了半天之后,我终于解决了这个问题。我无法使用.Include()和.Where()实现所需的结果,但这正是我所需要的。如果您能找到方法使其与.Include()和.Where()一起工作,请告诉我,因为我非常好奇:

return _db.Tasks
                    .Where(t => t.Category.ApplicationUser.UserName == username)
                    .Select(t => new
                    {
                        t,
                        t.Category
                    })
                    .AsEnumerable()
                    .Select(t => t.t)
                    .ToList();

在浪费了半天之后,我终于解决了这个问题。我无法使用.Include()和.Where()实现所需的结果,但这正是我所需要的。如果您能找到方法使其与.Include()和.Where()一起工作,请告诉我,因为我非常好奇:

return _db.Tasks
                    .Where(t => t.Category.ApplicationUser.UserName == username)
                    .Select(t => new
                    {
                        t,
                        t.Category
                    })
                    .AsEnumerable()
                    .Select(t => t.t)
                    .ToList();

例外情况是什么?还有,您使用的是什么版本的EF?显示您的
类别
应用程序用户
它是EF核心,异常附件您只显示堆栈跟踪。显示完整的异常消息“对象引用未设置为对象的实例”。异常是什么?还有,您使用的是什么版本的EF?显示您的
类别
应用程序用户
它是EF核心,异常附件您只显示堆栈跟踪。显示完整的异常消息“对象引用未设置为对象的实例”