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

完美的解釋,謝謝。 –