实体框架核心:InvalidOperationException



我正在尝试设置一种方法,该方法返回所有不与您注册的演示文稿重叠的演示文稿。但是,当尝试实施此功能时,我遇到了一个错误,我似乎无法解决,我环顾四周,找不到任何类似的问题。我想念明显的东西吗?

这是错误:

无效的exception:类型的变量't0' 'Microsoft.entityFrameWorkcore.Query.EntityQueryModelVisitor threntparentIdentifier`2 [namespace.models.presentation,Microsoft.entityframeworkcore.storage.valuebuffer]''']'''] 从范围引用'',但未定义

这些是我的模型。

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

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

如果要在DB中应用所有过滤 - 您应该将debug更改为"简单"的列表(IDS列表?) - 那么EF将能够将此列表传递回DB。

另外,您应该将所有合适的演示文稿读取到内存中(调用.ToList()),并在内存中应用最后的过滤。您可以从debug计算最小(启动时间)和最大(末端),然后将这两个简单值应用于演示文稿查询(您会收到较少的不必要的项目),然后读取内存并在内存中应用"强"过滤。

相关内容

  • 没有找到相关文章

最新更新