1

我试图设置一个方法,返回与您已注册的演示文稿不重叠的所有演示文稿。然而,当试图实现这个时,我遇到了一个我似乎无法修复的错误,我环顾四周,无法找到任何类似的问题。我错过了明显的东西吗?实体框架核心:InvalidOperationException

这是错误:

InvalidOperationException: variable 't0' of type 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[NameSpace.Models.Presentation,Microsoft.EntityFrameworkCore.Storage.ValueBuffer]' referenced from scope '', but it is not defined

这是我的模型。

public class ApplicationUser : IdentityUser 
{ 
    public List<Presentation> MyPresentations { get; set; } 

    public List<PresentationUser> RegisteredPresentations { get; set; } 
} 

public class Presentation 
{ 
    public int PresentationId { get; set; } 

    public string HostId { get; set; } 
    public ApplicationUser Host { get; set; } 

    public List<PresentationUser> Attendees { get; set; } 

    public int TimeId { get; set; } 
    public PresentationTime Time { get; set; } 
} 

public class PresentationUser 
{ 
    public int PresentationId { get; set; } 
    public Presentation Presentation { get; set; } 

    public string ApplicationUserId { get; set; } 
    public ApplicationUser ApplicationUser { get; set; } 
} 

public class PresentationTime 
{ 
    public int PresentationTimeId { get; set; } 
    public DateTime StartTime { get; set; } 
    public DateTime EndTime { get; set; } 
} 

这是我不能去上班

private async Task<IQueryable<Presentation>> GetAvailablePresentations() 
{ 
    User user = await context.Users 
     .Include(u => u.RegisteredPresentations) 
     .ThenInclude(eu => eu.Presentation.Host) 
     .FirstOrDefaultAsync(u => u.Id == userManager.GetUserId(User)); 


    var Presentations = context.Presentations 
     .Include(e => e.Host) 
     .Include(e => e.Time) 
     .Include(e => e.Attendees) 
     .ThenInclude(e => e.ApplicationUser) 
     // Filter Out Conditions 
     .Where(e => e.Attendees.All(u => u.ApplicationUserId != user.Id)) // Cannot see Presentations they are attending. 
     .Where(e => e.HostId != user.Id);         // Cannot see their own Presentation 

    var debug = user.RegisteredPresentations.Select(ex => ex.Presentation).ToList(); 

    // This section makes it so that users can't sign up for more that one Presentation per timeslot. 
    // Error Occurs Here 
    Presentations = Presentations.Where(e => debug.All(ex => 
     ex.Time.EndTime < e.Time.StartTime || e.Time.EndTime < ex.Time.StartTime)); 
    // This also does not work 
    // Presentations = Presentations.Where(e => debug.All(ex => ex.Time.StartTime != e.Time.StartTime)); 


    return Presentations; 
} 

如果有人能帮助我解决这个问题它会带来巨大帮助的方法。

注:我剥离了很多其他逻辑来帮助隔离此问题,因此我可能在此代码中有几个不必要的.Include()

回答

1

Presentations不是一个列表,它仍然是一个IQueryable - 一个尚未执行的数据库查询。应用Where指示EF在SQL中应用其他WHERE。 但debug是内存中的对象列表(.ToList())。你如何看待EF将他们转移回DB?

如果您想要在数据库中应用所有过滤器 - 您应该将debug更改为“简单”(id列表?)列表 - 那么EF将能够将此列表传递回数据库。

或者,您应该将所有适合的演示文稿读入内存(请拨打.ToList())并在内存中应用上次过滤。您可以从debug计算min(StartTime)和max(EndTime),并将这两个简单值应用于Presentations query(您将收到较少的不必要项目),然后读取到内存并在内存中应用“强大”过滤。

+0

完美的解释,谢谢。 –