简体   繁体   English

EntityFramework MySQL检索结果进行计数

[英]EntityFramework MySQL retrieves results for counting

I am using EntityFramework 6 with the official MySQL provider. 我正在使用带有官方MySQL提供程序的EntityFramework 6。

I have a database containing a list of VenuePlans which each consist of Areas. 我有一个数据库,其中包含一个VenuePlan列表,每个列表都包含Areas。 In order to show these values I am using this very simple LINQ query: 为了显示这些值,我使用了这个非常简单的LINQ查询:

model.VenuePlans = CurrentOrganization.VenuePlans.Select(p => new ViewModels.VenuePlans.IndexViewModel.VenuePlan
    {
        ID = p.MaskID,
        Name = p.DisplayName,
        AreaCount = p.VenuePlans_Areas.Count()
    }).ToArray();

But when looking at the executed queries using MiniProfiler I see that this results in duplicate queries as follows: 但是当使用MiniProfiler查看执行的查询时,我看到这会导致重复查询,如下所示:

Retrieving the VenuePlans: 检索场地计划:

SELECT
`Extent1`.`PlanID`, 
`Extent1`.`MaskID`, 
`Extent1`.`DisplayName`, 
`Extent1`.`OrganizationID`, 
`Extent1`.`SVG`
FROM `VenuePlans` AS `Extent1`
 WHERE `Extent1`.`OrganizationID` = @EntityKeyValue1

Retrieving the Areas for the first VenuePlan: 为第一个场馆计划检索区域:

SELECT
`Extent1`.`AreaID`, 
`Extent1`.`PlanID`, 
`Extent1`.`DisplayName`, 
`Extent1`.`MaskID`, 
`Extent1`.`FillColor`, 
`Extent1`.`InternalName`
FROM `VenuePlans_Areas` AS `Extent1`
 WHERE `Extent1`.`PlanID` = @EntityKeyValue1

Now this latter query is repeated for every area present in the database. 现在,对数据库中存在的每个区域重复进行后一个查询。

CurrentOrganization is an instance of another model retrieved earlier. CurrentOrganization是先前检索到的另一个模型的实例。 Now when writing the query directly on the DbContext instance I don't have this issue: 现在,当直接在DbContext实例上编写查询时,我没有这个问题:

model.VenuePlans = DbContext.VenuePlans
        .Where(p => p.OrganizationID == CurrentOrganization.OrganizationID)
        .Select(p => new ViewModels.VenuePlans.IndexViewModel.VenuePlan
        {
            ID = p.MaskID,
            Name = p.DisplayName,
            AreaCount = p.VenuePlans_Areas.Count()
        }).ToArray();

What is the reason for this? 这是什么原因呢?

DbContext is a variable declared in my BaseController which returns an instance of the current DbContext stored in HttpRequest.Items. DbContext是在我的BaseController中声明的变量,该变量返回存储在HttpRequest.Items中的当前DbContext的实例。

What can I do to prevent this behavior? 我该如何防止这种行为?

I've never found the MySql Linq stuff to be very good. 我从未发现MySql Linq的东西非常好。 I used it recently, and had to use ToList earlier than I would have liked to stop the query generation from spouting gibberish. 我最近使用过它,并且必须比要阻止查询生成停止乱码的更早使用ToList。

Armed with the knowledge that Linq to MySql is broken, and it's not just you, you'd be best using the version of the query that's fluid from your context instead of from your object. 掌握了Linq to MySql已损坏的知识,而不仅仅是您自己,您最好使用从上下文而不是从对象流动的查询版本。

Having said that, I'd be interesting in seeing if anybody does have a solution, because I tend to avoid Linq when using MySql. 话虽如此,我很想看看是否有人有解决方案,因为在使用MySql时我倾向于避免使用Linq。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM