实体框架、通用存储库模式和奇怪的 SQL 生成

发布于 2024-10-17 19:17:56 字数 1933 浏览 3 评论 0 原文

我已经为 Entity Framework 4 实现了一个通用存储库。这是一个简化版本,其中 AllAppContainer 是 EF4 对象上下文:

public class Repository<T> where T : class
{
    protected AllAppContainer objectContext;
    protected ObjectSet<T> entitySet;

    public Repository()
    {
        objectContext  = new AllAppContainer();
        entitySet = objectContext.CreateObjectSet<T>();
    }

    public int QueryCount(Func<T, bool> predicate)
    {
        int queryCount = entitySet.Count(predicate);
        return queryCount;
    }
}

其中一个方法是 QueryCount(),我想将其用作 select Count(*) 。 .. SQL 行(不返回实际记录)。

直截了当?您可能会想...首先,让我们执行相同操作的非存储库版本,对项目实体执行计数:

AllAppContainer allAppContainer = new AllAppContainer();
int nonRepCount = allAppContainer.Items.Count(item => item.Id > 0);

SQL Server Profiler 表示生成的 SQL 是:

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM [dbo].[Items] AS [Extent1]
    WHERE [Extent1].[Id] > 0
)  AS [GroupBy1]

Woo-hoo!分数!

现在,让我们使用我的存储库 QueryCount 调用相同的方法:

Repository<Item> repository = new Repository<Item>();
int repCount = repository.QueryCount(item => item.Id > 0);

这是生成的 SQL:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[SmallField] AS [SmallField]
FROM [dbo].[Items] AS [Extent1]

是的,EF 将返回完整的数据集,然后在内存中对其调用 Count()

为了好玩,我尝试将 Repository QueryCount 中的相关行更改为:

int queryCount = new AllAppContainer().CreateObjectSet<T>().Count(predicate);

,将非存储库行更改为:

int nonRepCount = allAppContainer1.CreateObjectSet<Item>().Count(item => item.Id > 0);

,但每个生成的 SQL 与以前相同。

现在为什么所有这些存储库返回所有匹配记录然后计数都会发生,而对于非存储库则不然?有没有什么方法可以通过我的通用存储库(即在 db.count 处进行计数)来做我想做的事情。我无法承受内存计数性能的下降。

I've implemented a generic repository for Entity Framework 4. Here's a dumbed down version, where AllAppContainer is the EF4 object context:

public class Repository<T> where T : class
{
    protected AllAppContainer objectContext;
    protected ObjectSet<T> entitySet;

    public Repository()
    {
        objectContext  = new AllAppContainer();
        entitySet = objectContext.CreateObjectSet<T>();
    }

    public int QueryCount(Func<T, bool> predicate)
    {
        int queryCount = entitySet.Count(predicate);
        return queryCount;
    }
}

The one method is QueryCount(), which I want to act as a select Count(*) ... where line of SQL (not returning the actual records).

Straight-forward? You'd think... First, let's do a non-Repository version of the same thing, performing a count on Item entities:

AllAppContainer allAppContainer = new AllAppContainer();
int nonRepCount = allAppContainer.Items.Count(item => item.Id > 0);

SQL Server Profiler says the generated SQL is:

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM [dbo].[Items] AS [Extent1]
    WHERE [Extent1].[Id] > 0
)  AS [GroupBy1]

Woo-hoo! Score!

Now let's call the same using my Repository QueryCount:

Repository<Item> repository = new Repository<Item>();
int repCount = repository.QueryCount(item => item.Id > 0);

Here's the generated SQL:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[SmallField] AS [SmallField]
FROM [dbo].[Items] AS [Extent1]

Yep, EF is returning the full set of data, then calling Count() on it in-memory.

For fun I tried changing the relevant line in Repository QueryCount to:

int queryCount = new AllAppContainer().CreateObjectSet<T>().Count(predicate);

and the non-repository line to:

int nonRepCount = allAppContainer1.CreateObjectSet<Item>().Count(item => item.Id > 0);

but the generated SQL for each is the same as before.

Now why would all this repository-returns-all-matching-records-then-counts be happening, when it doesn't for non-repository? And is there any way to do what I want via my generic repository i.e. count at db. I can't take the in-memory count performance hit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-10-24 19:17:56

您需要使用Expression>; Count 的谓词,否则框架将使用 Enumerable.Count方法 (IEnumerable, Func) 从数据库获取整个集合以便能够调用每个项目,因此您的方法应该是:

public int QueryCount(Expression<Func<T, Boolean>> predicate)
{
    int queryCount = entitySet.Count(predicate);
    return queryCount;
}

you need to use Expression<Func<TSource, bool>> predicate for your Count otherwise the framework uses the Enumerable.Count<TSource> Method (IEnumerable<TSource>, Func<TSource, Boolean>) which gets the whole collection from DB to be able to call for each item, so your method should be:

public int QueryCount(Expression<Func<T, Boolean>> predicate)
{
    int queryCount = entitySet.Count(predicate);
    return queryCount;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文