EF Core 的 IMethodCallTranslator 与 'EF 一起工作需要什么。函数'提供自定义功能?



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

我创建了:

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

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

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

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

不起作用:

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) });

最新更新