[英]Entity Framework Core Throwing Exception: The LINQ expression could not be translated with PostgreSQL
我正在嘗試使用 Entity Framework Core 3.1.8 和以下實體來過濾 ASP.NET Core 3.1 中的查詢:
public class ProductVariant
{
public Guid DepositId { get; set; }
public Collection<ProductVariantProperty> ProductVariantProperties { get; set; }
public int Quantity { get; set; }
}
public class ProductVariantProperty
{
public int ProductAttributeId { get; set; }
public int ProductAttributeValueId { get; set; }
}
This is the query I'm using:
var result = productVariantRepository
.Where(p => p.ProductVariantProperties.Any(x => x.ProductAttributeId == 12))
.ToList();
實體地圖:
builder.Entity<ProductVariant>(b =>
{
b.ToTable(CommerceConsts.DbTablePrefix + "ProductVariants", CommerceConsts.DbSchema);
b.ConfigureByConvention();
b.Property(x => x.ProductVariantProperties).HasColumnType("jsonb");
b.HasOne<Deposit>().WithMany().HasForeignKey(x => x.DepositId).IsRequired();
});
這是例外:
LINQ 表達式'DbSet .Where(p => p.ProductVariantProperties .Any(x => x.ProductAttributeId == 12))'無法翻譯。 以可翻譯的形式重寫查詢,或通過插入對 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的調用顯式切換到客戶端評估。
它僅在使用productVariantRepository.AsEnumerable() 時有效,但這會從表中返回所有對象。
主要是在 jsonb 中使用 Json 屬性,但只有查詢過濾器不起作用
https://www.npgsql.org/efcore/mapping/json.html?tabs=data-annotations%2Cpoco
LINQ 表達式 'DbSet .Where(p => p.ProductVariantProperties .Any(x => x.ProductAttributeId == 12))' 無法翻譯。
發生此錯誤是因為表ProductVariant
沒有列名ProductVariantProperties
。 您可以嘗試打開數據庫再次檢查。
這就是為什么您的 linq 無法轉換為 SQL 語句的原因。 喜歡的東西:沒有列名ProductVariantProperties
在表ProductVariant
。
我建議過濾包含ProductAttributeId
屬性的ProductVariantProperty
。 然后,將值分配給ProductVariantProperties
屬性。
ProductVariantProperty 沒有任何可用於連接這兩個表的字段。 將 ProductVariantId 添加到 ProductVariantProperty 並將這兩個表連接起來。 在此之后,您可以通過 ProductAttributeId 進行選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.