EF Core的IMethodCallTranslator与`EF.Functions`一起提供自定义函数需要什么? [英] What is required for EF Core's IMethodCallTranslator to work with `EF.Functions` to provide a custom function?

查看:145
本文介绍了EF Core的IMethodCallTranslator与`EF.Functions`一起提供自定义函数需要什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement a custom IMethodCallTranslator in EF Core 3.1 with the Sqlite provider.

我创建了:

  1. DbFunctions 的扩展方法,该扩展方法在查询时被调用
  2. IMethodCallTranslator 的实现,该实现不调用 Translate
  3. 派生的 RelationalMethodCallTranslatorProvider ,我正在传递我的 IMethodCallTranslator 的实例.这个构造器很受欢迎.我还尝试覆盖 RelationalMethodCallTranslatorProvider.Translate ,但没有成功.
  4. IDbContextOptionsExtension (及其信息类)的实现,该实现将 RelationalMethodCallTranslatorProvider 注册为单例 IMethodCallTranslatorProvider .
  1. An extension method off of this DbFunctions which is called at query time
  2. An implementation of IMethodCallTranslator which is Translate not called
  3. A derived RelationalMethodCallTranslatorProvider which I'm passing an instance of my IMethodCallTranslator. This constructor is hit. I also tried overriding RelationalMethodCallTranslatorProvider.Translate and this was not hit.
  4. An implementation of IDbContextOptionsExtension (and its info class) which registers the RelationalMethodCallTranslatorProvider as a singleton IMethodCallTranslatorProvider.

所有这些都是通过 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆