带有 Mongo 的 OData:System.ArgumentException:'Item' 类型上的属性 'Id' 必须是复杂属性。 (参数'propertyInfo')'

Posted

技术标签:

【中文标题】带有 Mongo 的 OData:System.ArgumentException:\'Item\' 类型上的属性 \'Id\' 必须是复杂属性。 (参数\'propertyInfo\')\'【英文标题】:OData with Mongo: System.ArgumentException: 'The property 'Id' on type 'Item' must be a Complex property. (Parameter 'propertyInfo')'带有 Mongo 的 OData:System.ArgumentException:'Item' 类型上的属性 'Id' 必须是复杂属性。 (参数'propertyInfo')' 【发布时间】:2022-01-12 12:22:18 【问题描述】:

我正在尝试使用ODataMongo 配置.net core

我收到此错误:System.ArgumentException: 'The property 'Id' on type 'Item' must be a Complex property. (Parameter 'propertyInfo')'

这是我的配置:

services.AddControllers(options =>
            
               // something here
            )
            .AddOData(opt =>
            
                opt.Count()
                    .Filter()
                    .Expand()
                    .Select()
                    .OrderBy()
                    .SetMaxTop(5000)
                    .AddRouteComponents("odata", GetEdmModel());

                opt.TimeZone = TimeZoneInfo.Utc;
            );

public static IEdmModel GetEdmModel()
    
        var builder = new ODataConventionModelBuilder();

        var entitySetConfiguration = builder.EntitySet<Item>("Item");
        entitySetConfiguration.EntityType.HasKey(entity => entity.Id);

        return builder.GetEdmModel();
    

还有我的实体:

public class Item : Document

    public string SubCategory  get; set; 
    public string ProductCode  get; set; 
    public string Name  get; set; 
    public string Description  get; set; 


public abstract class Document : IDocument

    public ObjectId Id  get; set; 

    public DateTime CreatedAt => Id.CreationTime;

【问题讨论】:

【参考方案1】:
public abstract class Document : IDocument

    //...
    public string IdStr => Id.ToString();

【讨论】:

以上是关于带有 Mongo 的 OData:System.ArgumentException:'Item' 类型上的属性 'Id' 必须是复杂属性。 (参数'propertyInfo')'的主要内容,如果未能解决你的问题,请参考以下文章