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
.
I'm trying to implement a custom IMethodCallTranslator
in EF Core 3.1 with the Sqlite provider.
我创建了:
- 此 DbFunctions 的扩展方法,该扩展方法在查询时被调用
-
IMethodCallTranslator
的实现,该实现不调用Translate
- 派生的
RelationalMethodCallTranslatorProvider
,我正在传递我的IMethodCallTranslator
的实例.这个构造器很受欢迎.我还尝试覆盖RelationalMethodCallTranslatorProvider.Translate
,但没有成功. -
IDbContextOptionsExtension
(及其信息类)的实现,该实现将RelationalMethodCallTranslatorProvider
注册为单例IMethodCallTranslatorProvider
.
- An extension method off of
this DbFunctions
which is called at query time - An implementation of
IMethodCallTranslator
which isTranslate
not called - A derived
RelationalMethodCallTranslatorProvider
which I'm passing an instance of myIMethodCallTranslator
. This constructor is hit. I also tried overridingRelationalMethodCallTranslatorProvider.Translate
and this was not hit. - An implementation of
IDbContextOptionsExtension
(and its info class) which registers theRelationalMethodCallTranslatorProvider
as a singletonIMethodCallTranslatorProvider
.
所有这些都是通过 IDbContextOptionsBuilderInfrastructure
形式的选项通过 OnConfiguring
添加的.
All of this is added via OnConfiguring
by getting the IDbContextOptionsBuilderInfrastructure
form of the options.
我错过了什么吗?我已经尝试过以下操作: https://github.com/tkhadimullin/ef-core-custom-functions/tree/feature/ef-3.1-version ,但是它正在调用扩展方法,而不是我的翻译器.
Am I missing something? I've tried following: https://github.com/tkhadimullin/ef-core-custom-functions/tree/feature/ef-3.1-version in my code, yet it's invoking the extension method, not my translator.
推荐答案
显然,您不能将对完整实体类的引用传递给函数以进行进一步处理:
Apparently, you can't pass a reference to your full entity class to the function for further processing:
不起作用:
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
中获取模型的实体类型的一种好方法.
Also, generics are supported so it would be a nice way to introspect call and get the type of the entity for the model in a RelationalMethodCallTranslatorProvider
.
public static string DoWork<T>(this DbFunctions _, string key)
//...
dbContext.Items.Select(i => new { Entity = i, String = EF.Functions.DoWork<Item>(i.StringValue) });
这篇关于EF Core的IMethodCallTranslator与`EF.Functions`一起提供自定义函数需要什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!