简体   繁体   English

Entity Framework Core 抛出异常:无法使用 PostgreSQL 翻译 LINQ 表达式

[英]Entity Framework Core Throwing Exception: The LINQ expression could not be translated with PostgreSQL

I'm trying to filter a query in ASP.NET Core 3.1, using Entity Framework Core 3.1.8 with the entities bellow:我正在尝试使用 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();

Entity Map:实体地图:

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

This is the exception:这是例外:

The LINQ expression 'DbSet .Where(p => p.ProductVariantProperties .Any(x => x.ProductAttributeId == 12))' could not be translated . LINQ 表达式'DbSet .Where(p => p.ProductVariantProperties .Any(x => x.ProductAttributeId == 12))'无法翻译 Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。

It only works when using productVariantRepository.AsEnumerable() , but this returns all object from the table.它仅在使用productVariantRepository.AsEnumerable() 时有效,但这会从表中返回所有对象。

The main go is to use Json property with jsonb, but only the query filter is not working主要是在 jsonb 中使用 Json 属性,但只有查询过滤器不起作用

https://www.npgsql.org/efcore/mapping/json.html?tabs=data-annotations%2Cpoco https://www.npgsql.org/efcore/mapping/json.html?tabs=data-annotations%2Cpoco

The LINQ expression 'DbSet .Where(p => p.ProductVariantProperties .Any(x => x.ProductAttributeId == 12))' could not be translated. LINQ 表达式 'DbSet .Where(p => p.ProductVariantProperties .Any(x => x.ProductAttributeId == 12))' 无法翻译。

This error occurs because the table ProductVariant doesn't have column name ProductVariantProperties .发生此错误是因为表ProductVariant没有列名ProductVariantProperties You can try to open database to check again.您可以尝试打开数据库再次检查。

That's why your linq cannot be translated to SQL statement.这就是为什么您的 linq 无法转换为 SQL 语句的原因。 Something like: There is no column name ProductVariantProperties in the table ProductVariant .喜欢的东西:没有列名ProductVariantProperties在表ProductVariant

I suggest to filter ProductVariantProperty that including ProductAttributeId property.我建议过滤包含ProductAttributeId属性的ProductVariantProperty Then, assigning the value to ProductVariantProperties property.然后,将值分配给ProductVariantProperties属性。

ProductVariantProperty doesn't have any field that can be used to connect this two tables. ProductVariantProperty 没有任何可用于连接这两个表的字段。 Add ProductVariantId to ProductVariantProperty and join this two tables them.将 ProductVariantId 添加到 ProductVariantProperty 并将这两个表连接起来。 After this you can select by ProductAttributeId.在此之后,您可以通过 ProductAttributeId 进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM