Odata 版本 8.2.3 存在 NotMapped 属性问题,无法使用 notmapped 字段过滤数据

问题描述 投票:0回答:1

Odata 版本 8.2.3 存在 [NotMapped] 属性问题,无法使用 notmapped 字段过滤数据。

public partial class Customer
{
    
    [Newtonsoft.Json.JsonProperty("Name", Required = Newtonsoft.Json.Required.Always)]
    [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
    public string Name { get; set; }

    [Newtonsoft.Json.JsonProperty("ShortName", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public string ShortName { get; set; }

    [Newtonsoft.Json.JsonProperty("Description", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public string Description { get; set; }

    [Newtonsoft.Json.JsonProperty("Status", Required = Newtonsoft.Json.Required.Always)]
    public WorksWarefx.Core.Enums.RecordStatus Status { get; set; } =    WorksWarefx.Core.Enums.RecordStatus.active;

    
    [NotMapped]
    [Newtonsoft.Json.JsonProperty("StatusId", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public int StatusId
    {
        get
        {
            return (int)Status;
        }
    }

}

EDM模型注册如下。

StructuralTypeConfiguration<Customer> customer = builder.EntitySet<Customer>(nameof(Customer)).EntityType;
customer.Property(c => c.StatusId);

下面是LINQ抛出的异常,

Error Name : System.InvalidOperationException
Error Message : The LINQ expression 'DbSet<Customer>()
.Where(s => s.StatusId == __TypedProperty_0)' could not be 
translated. Additional information: Translation of member `'StatusId' on entity type 'Customer' failed. This commonly occurs when the specified member is unmapped. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.`

Exception Type : System.InvalidOperationException

对此的回应总是有帮助的..

我们尝试删除 EDM 模型

"EntityType.Count().Select().Filter().OrderBy().Expand(9)"

从注册表但不起作用。

c# linq entity-framework-core odata .net-8.0
1个回答
0
投票

记录

EnableQuery 属性是一个操作过滤器,用于解析、验证、 并应用查询。过滤器将查询选项转换为 LINQ(语言集成查询)表达式。当控制器 返回 IQueryable 或 IActionResult 类型,LINQ 提供程序 将 LINQ 表达式转换为查询,例如实体框架 (EF) Core 会将 LINQ 表达式转换为 SQL 语句。

在你的例子中,你返回了一个 OkObject 结果,如果你尝试使用你的过滤器,它会被转换为 linq,如下所示:

_context.Customer.Where(x => x.StatusId == 1);

但是db中没有StatusId列,所以无法翻译成sql

SELECT ... FROM CUSTOMERS WHERE STATUSId =...

您可以尝试在 dbcontext 中配置 ComputedColumn

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Customer>().Property(x => x.StatusId).HasComputedColumnSql("...");
}
© www.soinside.com 2019 - 2024. All rights reserved.