Hello,
I used MongoDB for the database and OData on my project
I have a model that has an open type is Dictionary<string, object>
public class DictionaryModel : Entity
{
public DictionaryModel()
{
Id = Guid.NewGuid();
Name = Guid.NewGuid().ToString();
Properties = new Dictionary<string, object>();
}
public Guid Id { get; set; }
public string Name { get; set; }
[BsonExtraElements]
public IDictionary<string, object> Properties { get; set; }
}
My controller:
private readonly IDictionaryRepository _repository;
public DictionaryController(IDictionaryRepository repository)
{
_repository = repository;
if (!repository.AsQueryable().Any())
{
var data = new DictionaryModel();
data.Properties.Add("String", "string 1");
data.Properties.Add("Number", 1);
data.Properties.Add("Boolean", true);
var data2 = new DictionaryModel();
data2.Properties.Add("String", "string 2");
data2.Properties.Add("Number", 2);
data2.Properties.Add("Boolean", false);
_repository.AddAsync(data).ConfigureAwait(false).GetAwaiter().GetResult();
_repository.AddAsync(data2).ConfigureAwait(false).GetAwaiter().GetResult();
}
}
[HttpGet]
[EnableQuery]
public IQueryable<DictionaryModel> Get()
{
return _repository.AsQueryable();
}
My ConfigureServices:
services.AddControllers().AddOData(opt =>
{
opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(5000).AddRouteComponents("odata", GetEdmModel());
opt.TimeZone = TimeZoneInfo.Utc;
});
My EDM Model:
private IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<DictionaryModel>("Dictionary");
odataBuilder.EntityType<DictionaryModel>().HasKey(entity => entity.Id);
return odataBuilder.GetEdmModel();
}
My Configure:
app.UseODataRouteDebug();
// Add OData /$query middleware
app.UseODataQueryRequest();
// Add the OData Batch middleware to support OData $Batch
app.UseODataBatching();
When I send request to URL: /odata/Dictionary?$filter=String eq ‘string 1’
I received an error:
Convert(IIF((({document}{Properties} != null) AndAlso {document}{Properties}.ContainsKey(“String”)), {document}{Properties}.Item[“String”], null), String) is not supported.
I use:
Microsoft.AspNetCore.OData - v8.0.2
MongoDB.Driver - v2.13.1
How do I fix this issue?