1

In EF Core, I'm trying to map a custom function to an InExpression using the HasDbFunction functionality. I'm having trouble getting it to work as EF keeps complaining:

'The parameter 'enumerable' for the DbFunction 'StringEnumerableContains(IEnumerable,string)' has an invalid type 'IEnumerable'. Ensure the parameter type can be mapped by the current provider.'

My custom function is such:

public static bool StringEnumerableContains(IEnumerable<string> enumerable, string value)
{
    return enumerable.Contains(value, StringComparer.OrdinalIgnoreCase);
}

And I'm trying to map it as so:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    var stringEnumerableContainsMethod = typeof(Util).GetMethods(BindingFlags.Static | BindingFlags.Public).FirstOrDefault(m => m.Name == "StringEnumerableContains");
    modelBuilder.HasDbFunction(stringEnumerableContainsMethod).HasTranslation(args => new InExpression(args.Skip(1).First(), args.First(), false, null));       
}

I've also tried making the custom function bool StringEnumerableContains(object enumerable, string value) and it similarly says it cannot map type "object".

Background: I need to map this custom db function because it needs to work in-memory as well as in EF Core. So the best way to achieve case-insensitivity in memory is by implementing it myself and in EF by mapping it to a normal IN expression (we use a case insensitive collation).

Any ideas for how to get this HasDbFunction mapping working correctly?

MgSam
  • 12,139
  • 19
  • 64
  • 95

0 Answers0