I have an entity with properties of type JObject and I need to be able to use DbFunctions against those properties.
When I execute, the project throws an exception saying that DbFunction doesn't allows parameters of type JObject.
The entity is like...
public class OrchestrationRun
{
public long Id { get; set; }
public JObject MetaData { get; set; }
public JObject SystemMetaData { get; set; }
}
The DbContext looks like...
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions options)
: base(options)
{
}
public virtual DbSet<OrchestrationRun> OrchestrationRun { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new OrchestrationRunConfiguration());
// DbFunction mapping for JSON_VALUE
modelBuilder.HasDbFunction( typeof(MyDbContext).GetMethod(nameof(JsonValue)))
.HasName("JSON_VALUE")
.HasSchema("");
}
// DbFunction
public static string JsonValue(JObject column, [NotParameterized] string path) => throw new NotSupportedException();
}
The OrchestrationRunConfiguration is ...
public class OrchestrationRunConfiguration : IEntityTypeConfiguration<OrchestrationRun>
{
public void Configure(EntityTypeBuilder<OrchestrationRun> builder)
{
builder.Property(e => e.MetaData).HasConversion(
jObject => jObject != null ? jObject.ToString(Formatting.None) : null,
json => string.IsNullOrWhiteSpace(json) ? null : JObject.Parse(json)
);
builder.Property(e => e.SystemMetaData).HasConversion(
jObject => jObject != null ? jObject.ToString(Formatting.None): null,
json => string.IsNullOrWhiteSpace(json) ? null : JObject.Parse(json)
);
}
}
The query I'm trying to execute is...
var dbResponse = (from or in this.dbContext.OrchestrationRun
where MyDbContext.JsonValue(or.MetaData,"$.Product.ProductCategoryName") == "EXAMPLE"
select new
{
Id = or.Id,
CategoryId = "EXAMPLE"
}
).ToList();
Note: The exception occurs at DbContext instantiation. Therefore the query is never called.
The exception thrown is...
System.InvalidOperationException: The parameter 'column' for the DbFunction 'MyDbContext.JsonValue' has an invalid type 'JObject'. Ensure the parameter type can be mapped by the current provider. at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateDbFunctions(IModel model) at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel model) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ValidatingConvention.Apply(InternalModelBuilder modelBuilder)