简体   繁体   English

连接字符串中缺少MultipleActiveResultSets导致错误

[英]Missing MultipleActiveResultSets from the connection string causing an error

I have a table with Subscribers. 我有一张与订户有关的表。 Each subscriber is following Targets since a certain date. 自某个日期以来,每个订户都在关注目标。 New information about the targets is being accumulated in a table named TargetDatas. 有关目标的新信息将累积在名为TargetDatas的表中。

I want to create a list of Subscribers that need to get a report, which contains the updates about the targets since the last report/or if no report has ever been issued since the date the target is being followed. 我想创建一个需要获取报告的订户列表,其中包含自上次报告以来的有关目标的更新,或者自遵循目标之日起是否尚未发布任何报告。

I tried for 2 days to figure out a linq query to get that list and failed. 我尝试了2天,以找出一个linq查询来获取该列表,但失败了。 The only way I can get the list is like this: 我可以获取列表的唯一方法是这样的:

List<Subscriber> outlistl = new List<Subscriber>();

foreach (Subscriber s in db.Subscribers)
{
    bool foundUpdate = false;

    foreach (TargetSubscriber ts in s.TargetsX)
    {
        //has this subscriber received a report in the past
        //if yes, since that date
        //otherwise, since the target is being watched
        DateTime fromDate;

        var latestReport = s.SubscriberReports.OrderByDescending(sr => sr.ToDate).FirstOrDefault();

        if (latestReport != null)
        {
            fromDate = latestReport.ToDate;
        }
        else
        {
            fromDate = ts.CreatedDate;
        }

        foreach (TargetData td in ts.Target.TargetDatas)
        {
            if (td.CreatedDate > fromDate)
            {
                outlistl.Add(s);
                foundUpdate = true;
                break;
            }
        }
        if (foundUpdate) { break; }
    }
    if (foundUpdate) { break; }
}

When I ran it the first time I got an error: 第一次运行时,出现错误:

There is already an open DataReader associated with this Command which must be closed first. 已经有一个与此命令相关联的打开的DataReader,必须先关闭它。

After some research I found that adding MultipleActiveResultSets=True; 经过一番研究,我发现添加MultipleActiveResultSets=True; to my connectionstring got rid of that error and I get the results that I need. 我的连接字符串摆脱了该错误,我得到了我需要的结果。

There has to be a better way. 一定有更好的方法。 Could someone please enlighten me on how this ought to be done, without me having set MultipleActiveResultsSets to true. 在没有将MultipleActiveResultsSets设置为true的情况下,有人可以启发我如何完成此工作。

It's common practice to have MultipleActiveResultSets=True . 具有MultipleActiveResultSets=True的常见做法是。 It's often necessary for EF to be able to perform lazy loading of navigation properties of objects it is materializing. EF通常必须能够对正在实现的对象执行导航属性的延迟加载。

But I think your query should look like this: 但我认为您的查询应如下所示:

from s in db.Subscribers
from ts in s.TargetsX
let fromDate = s.SubscriberReports.Select(sr => (DateTime?)sr.ToDate)
                .OrderByDescending(d => d)
                .FirstOrDefault() ?? ts.CreatedDate
where ts.Target.TargetDatas.Any(td => td.CreatedDate > fromDate)
select s

I cast sr.ToDate to DateTime? 我将sr.ToDateDateTime? to allow using the ?? 允许使用?? operator. 操作员。 Now if there are no SubscriberReports the query for sr.ToDate will return a null date. 现在,如果没有SubscriberReports ,则sr.ToDate的查询将返回空日期。 (In fact it's only a trick to make the compiler happy, for the generated SQL query it wouldn't make any difference). (实际上,这只是使编译器满意的一种技巧,因为生成的SQL查询不会有任何区别)。

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

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