2

我正在使用 SQL Azure 开发一个项目。为了处理 Sql Azure 瞬态错误,我正在使用瞬态故障处理框架。

我有一个继承自 DbContext 的类 MContext 我在这个类中为实体框架 SaveChanges() 方法编写了一个方法覆盖,如下所示:

private ITransientErrorDetectionStrategy errorDetectionStrategy = new SqlAzureTransientErrorDetectionStrategy();

private RetryPolicy policy= new RetryPolicy(errorDetectionStrategy, 3);

 public override int SaveChanges()
        {
            return policy.ExecuteAction(() =>
            {
                return base.SaveChanges();
            });
        }  

这样我就不必在调用 SaveChanges() 的整个项目中进行代码更改。

我想知道是否有办法对 LINQ 查询做同样的事情。

例如:

policy.ExecuteAction(() =>
            {
                var x = from user in context.Users select user.Id;
            });

而不是通过添加

policy.ExecuteAction(() =>
            {
}); 

阻止所有 linq 查询并强制团队中的其他人也这样做,有没有办法在 MContext 类中拦截 linq 查询并应用瞬态处理?

我可以编写一个扩展方法并在所有查询上调用它,但同样需要在整个项目中更改代码。

我想在一个地方处理它,就像覆盖 SaveChanges() 方法一样,而不是在整个项目中进行更改。

谢谢你的帮助!马赫什

4

0 回答 0