我正在尝试使用Sqlite提供程序在EF Core 3.1中实现自定义IMethodCallTranslator
。
我创建了:
this DbFunctions
的扩展方法,在查询时调用- 未调用
Translate
的IMethodCallTranslator
的实现 - 一个派生的
RelationalMethodCallTranslatorProvider
,我正在传递我的IMethodCallTranslator
的一个实例。此构造函数已命中。我还尝试覆盖RelationalMethodCallTranslatorProvider.Translate
,但没有成功 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) });