EF Core 的 IMethodCallTranslator 需要什么才能与“EF.Functions”一起使用以提供自定义函数?

What is required for EF Core's IMethodCallTranslator to work with `EF.Functions` to provide a custom function?

我正在尝试使用 Sqlite 提供程序在 EF Core 3.1 中实现自定义 IMethodCallTranslator

我创建了:

  1. this DbFunctions的扩展方法,在查询时调用
  2. IMethodCallTranslator 的实现 Translate 未调用
  3. 派生的 RelationalMethodCallTranslatorProvider,我正在传递我的 IMethodCallTranslator 的一个实例。这个构造函数命中。我也尝试覆盖 RelationalMethodCallTranslatorProvider.Translate 但没有命中。
  4. IDbContextOptionsExtension(及其信息 class)的实现,将 RelationalMethodCallTranslatorProvider 注册为单例 IMethodCallTranslatorProvider

所有这些都是通过 OnConfiguring 通过获取选项的 IDbContextOptionsBuilderInfrastructure 形式添加的。

我错过了什么吗?我已尝试以下操作:https://github.com/tkhadimullin/ef-core-custom-functions/tree/feature/ef-3.1-version 在我的代码中,但它调用的是扩展方法,而不是我的翻译器。

显然,您不能将对完整实体的引用 class 传递给函数以供进一步处理:

无效:

public static string DoWork(this DbFunctions _, object entity, string key)
//...
dbContext.Items.Select(i => new { Entity = i, String = EF.Functions.DoWork(i, i.StringValue) });

这将:

public static string DoWork(this DbFunctions _, string key)
//...
dbContext.Items.Select(i => new { Entity = i, String = EF.Functions.DoWork(i.StringValue) });

此外,泛型 受到 支持,因此这将是一种很好的方式来内省调用并获取 RelationalMethodCallTranslatorProvider 中模型的实体类型。

public static string DoWork<T>(this DbFunctions _, string key)
//...
dbContext.Items.Select(i => new { Entity = i, String = EF.Functions.DoWork<Item>(i.StringValue) });