修改Entity Framework的表达式树,尽量接近T-SQL translation\execution
Modify Entity Framework's expression tree as close as possible to T-SQL translation\execution
我已经能够使用 ExpressionVisitor
和其他自定义 Expressions
.
修改 IQueryable
表达式
我的问题是使用 Entity Framework 的第三方框架(例如 OData)在内部方法中修改查询,并且在修改查询后我很难重新修改查询的地方。
在该过程的最后,有一个 IQueryable
表示表达式树。 Entity Framework 知道如何将该表达式树翻译成 T-SQL 并执行它。
我希望修改 Expression\IQueryable
尽可能接近执行。
最好的方法是什么?
可以使用Entity Framework拦截。这允许您在执行之前拦截所有查询(包括导航 属性 查询)
Entity Framework 允许我们在查询生成的不同方面指定不同类型的拦截器。每个拦截器都可以修改执行的查询。
拦截器类型有:
IDbCommandInterceptor
执行查询时将调用此拦截器的方法。查询将已经在 SQL 中转换,并且将设置参数。
IDbCommandTreeInterceptor
创建命令树时将调用此拦截器的方法。命令树是命令的 AST 表示。生成了两棵命令树,一个用概念模型表示(DataSpace.CSpace
),这个命令树会更接近LINQ查询,另一个用存储模型表示(DataSpace.CSpace
) 14=])
IDbConfigurationInterceptor
加载 DbConfiguration
时调用此拦截器的方法。
IDbConnectionInterceptor
当建立连接和发生事务时调用此拦截器的方法。
IDbTransactionInterceptor
当提交或回滚事务时调用此拦截器的方法。
IDbCommandTreeInterceptor
提供了一种获取命令并进行更改的好方法。 AST 非常容易理解,Entity Framework 已经提供了基于现有命令 AST 创建新命令 AST 的基础设施(命令 AST 是一个不可变的结构,因此我们不能只更改现有的)。
用法示例:
class CustomExpressionVisitor : DefaultExpressionVisitor
{
// Override method to mutate the query
}
class TestInterceptor : IDbCommandTreeInterceptor
{
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
{
if (interceptionContext.Result.DataSpace == DataSpace.CSpace)
{
// We only process query command trees
// (there can be others such as insert, update or delete
var queryCommand = interceptionContext.Result as DbQueryCommandTree;
if (queryCommand != null)
{
// A bit of logging to see the original tree
Console.WriteLine(queryCommand.DataSpace);
Console.WriteLine(queryCommand);
// We call the accept method on the command expression with our new visitor.
// This method will return our new command expression with the changes the
// visitor has made to it
var newQuery = queryCommand.Query.Accept(new CustomExpressionVisitor());
// We create a new command with our new command expression and tell
// EF to use it as the result of the query
interceptionContext.Result = new DbQueryCommandTree
(
queryCommand.MetadataWorkspace,
queryCommand.DataSpace,
newQuery
);
// A bit of logging to see the new command tree
Console.WriteLine(interceptionContext.Result);
}
}
}
}
// In code before using any EF context.
// Interceptors are registered globally.
DbInterception.Add(new TestInterceptor());
注意:查询计划被缓存,所以拦截不会在第一次遇到查询时被调用并被缓存(你指定的结果将被缓存而不是原始结果)。因此,这可以安全地用于不依赖于上下文的更改(例如:用户、请求、语言)。
我已经能够使用 ExpressionVisitor
和其他自定义 Expressions
.
IQueryable
表达式
我的问题是使用 Entity Framework 的第三方框架(例如 OData)在内部方法中修改查询,并且在修改查询后我很难重新修改查询的地方。
在该过程的最后,有一个 IQueryable
表示表达式树。 Entity Framework 知道如何将该表达式树翻译成 T-SQL 并执行它。
我希望修改 Expression\IQueryable
尽可能接近执行。
最好的方法是什么?
可以使用Entity Framework拦截。这允许您在执行之前拦截所有查询(包括导航 属性 查询)
Entity Framework 允许我们在查询生成的不同方面指定不同类型的拦截器。每个拦截器都可以修改执行的查询。 拦截器类型有:
IDbCommandInterceptor
执行查询时将调用此拦截器的方法。查询将已经在 SQL 中转换,并且将设置参数。IDbCommandTreeInterceptor
创建命令树时将调用此拦截器的方法。命令树是命令的 AST 表示。生成了两棵命令树,一个用概念模型表示(DataSpace.CSpace
),这个命令树会更接近LINQ查询,另一个用存储模型表示(DataSpace.CSpace
) 14=])IDbConfigurationInterceptor
加载DbConfiguration
时调用此拦截器的方法。IDbConnectionInterceptor
当建立连接和发生事务时调用此拦截器的方法。IDbTransactionInterceptor
当提交或回滚事务时调用此拦截器的方法。
IDbCommandTreeInterceptor
提供了一种获取命令并进行更改的好方法。 AST 非常容易理解,Entity Framework 已经提供了基于现有命令 AST 创建新命令 AST 的基础设施(命令 AST 是一个不可变的结构,因此我们不能只更改现有的)。
用法示例:
class CustomExpressionVisitor : DefaultExpressionVisitor
{
// Override method to mutate the query
}
class TestInterceptor : IDbCommandTreeInterceptor
{
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
{
if (interceptionContext.Result.DataSpace == DataSpace.CSpace)
{
// We only process query command trees
// (there can be others such as insert, update or delete
var queryCommand = interceptionContext.Result as DbQueryCommandTree;
if (queryCommand != null)
{
// A bit of logging to see the original tree
Console.WriteLine(queryCommand.DataSpace);
Console.WriteLine(queryCommand);
// We call the accept method on the command expression with our new visitor.
// This method will return our new command expression with the changes the
// visitor has made to it
var newQuery = queryCommand.Query.Accept(new CustomExpressionVisitor());
// We create a new command with our new command expression and tell
// EF to use it as the result of the query
interceptionContext.Result = new DbQueryCommandTree
(
queryCommand.MetadataWorkspace,
queryCommand.DataSpace,
newQuery
);
// A bit of logging to see the new command tree
Console.WriteLine(interceptionContext.Result);
}
}
}
}
// In code before using any EF context.
// Interceptors are registered globally.
DbInterception.Add(new TestInterceptor());
注意:查询计划被缓存,所以拦截不会在第一次遇到查询时被调用并被缓存(你指定的结果将被缓存而不是原始结果)。因此,这可以安全地用于不依赖于上下文的更改(例如:用户、请求、语言)。