[英]EFCore 3 with where condition throws exception
這很奇怪。 我有一個看起來像這樣的模型:
public class UserLimit: IDbItem //IDbItem has just Id field
{
public virtual Guid Id { get; set; }
public virtual Guid UserId { get; set; }
public virtual LimitTypes LimitType { get; set; } //some enum
public virtual DateTimeOffset? ValidUntil { get; protected set; }
int CurrentLimit { get; set; }
int PreviousLimit { get; set; }
}
請注意,為了清楚起見,還刪除了一些其他功能。 現在,我這樣映射它:
(b 是 EntityTypeBuilder)
b.ToTable("users_limits")
.HasKey(x => new { x.UserId, x.LimitType });
b.Property(x => x.UserId).IsRequired();
b.Property(x => x.LimitType).IsRequired().HasConversion(typeof(int));
b.Property(x => x.ValidUntil);
b.Property<int>("CurrentLimit").IsRequired().UsePropertyAccessMode(PropertyAccessMode.Field);
b.Property<int>("PreviousLimit").IsRequired().UsePropertyAccessMode(PropertyAccessMode.Field);
我需要從數據庫中獲取一些值:
var result = dbContext.UserLimits.AsQueryable()
.Where(x => x.UserId == userId && x.LimitType == limitType);
目前,一切正常。 當我在調試窗口中預覽結果時,我可以看到只有一個我期望的記錄。 但是,問題就在這里:
UserLimit ul = result.ElementAt(0);
盡管有對象,但我得到了一個例外:
Processing of the LINQ expression 'DbSet<UserLimit>
.Where(x => x.UserId == __userId_0 && (int)x.LimitType == (int)__limitType_1)
.ElementAt(__p_2)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
我的第一個想法是它與客戶端評估有關。 但是在客戶端根本沒有工作要做。 這只是一個簡單的查詢,應該只從一張表中選擇記錄。
ElementAt(0)
不是有效的 LINQ 可翻譯函數。 在這種情況下,您應該使用SingleOrDefault()
或Single()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.