2016-08-22 2 views
1

Это странно, может кто-нибудь попробует объяснить мой, почему это работает просто отлично:Включить() с п Wheres неудачу

_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(); 

По какой-то причине. Include() не работает с .Where clause. Есть идеи?

Исключение:

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 

генерируемый SQL, который работает просто отлично при выполнении против Sql Server:

SELECT [t].[TaskId], [t].[CategoryId], [t].[Description], [t].[Name], [t].[Timestamp], [t.Category].[CategoryId], [t.Category].[Description], [t.Category].[Name], [t.Category].[Timestamp], [t.Category].[UserId], [t.Category.ApplicationUser].[Id], [t.Category.ApplicationUser].[AccessFailedCount], [t.Category.ApplicationUser].[ConcurrencyStamp], [t.Category.ApplicationUser].[Email], [t.Category.ApplicationUser].[EmailConfirmed], [t.Category.ApplicationUser].[LockoutEnabled], [t.Category.ApplicationUser].[LockoutEnd], [t.Category.ApplicationUser].[NormalizedEmail], [t.Category.ApplicationUser].[NormalizedUserName], [t.Category.ApplicationUser].[PasswordHash], [t.Category.ApplicationUser].[PhoneNumber], [t.Category.ApplicationUser].[PhoneNumberConfirmed], [t.Category.ApplicationUser].[SecurityStamp], [t.Category.ApplicationUser].[TwoFactorEnabled], [t.Category.ApplicationUser].[UserName], [c].[CategoryId], [c].[Description], [c].[Name], [c].[Timestamp], [c].[UserId], [a].[Id], [a].[AccessFailedCount], [a].[ConcurrencyStamp], [a].[Email], [a].[EmailConfirmed], [a].[LockoutEnabled], [a].[LockoutEnd], [a].[NormalizedEmail], [a].[NormalizedUserName], [a].[PasswordHash], [a].[PhoneNumber], [a].[PhoneNumberConfirmed], [a].[SecurityStamp], [a].[TwoFactorEnabled], [a].[UserName] 
FROM [Tasks] AS [t] 
INNER JOIN [Categories] AS [t.Category] ON [t].[CategoryId] = [t.Category].[CategoryId] 
LEFT JOIN [AspNetUsers] AS [t.Category.ApplicationUser] ON [t.Category].[UserId] = [t.Category.ApplicationUser].[Id] 
INNER JOIN [Categories] AS [c] ON [t].[CategoryId] = [c].[CategoryId] 
LEFT JOIN [AspNetUsers] AS [a] ON [c].[UserId] = [a].[Id] 
ORDER BY [t.Category].[UserId] 

Модели:

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; } 
} 
+0

Какое исключение? И какая версия EF вы используете? – octavioccl

+0

Показать ваши 'Category' и' ApplicationUser' – haim770

+0

Это EF Core, исключение включено – lucas

ответ

0

Наконец, я был в состоянии решить вопрос после половины дня впустую. Я не смог достичь желаемых результатов с помощью .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(); 
Смежные вопросы