【问题标题】:Same data being returned by linq for 2 different executions of a stored procedure?linq 为存储过程的 2 次不同执行返回相同的数据?
【发布时间】:2011-01-13 23:51:33
【问题描述】:

我有一个通过实体框架调用的存储过程。

存储过程有 2 个日期参数。我在调用存储过程的 2 次中提供了不同的参数。我已经使用 SQL Profiler 验证了存储过程被正确调用并返回了正确的结果。

当我第二次使用不同的参数调用我的方法时,即使存储过程返回了正确的结果,创建的表也包含与我第一次调用它时相同的数据。

dtStart = 01/08/2009  
dtEnd = 31/08/2009

public List<dataRecord> GetData(DateTime dtStart, DateTime dtEnd)
{
  var tbl = from t in db.SP(dtStart, dtEnd)                       
                      select t;
  return tbl.ToList();            
}

GetData((new DateTime(2009, 8, 1), new DateTime(2009, 8, 31))
// tbl.field1 value = 45450 - CORRECT

GetData(new DateTime(2009, 7, 1), new DateTime(2009, 7, 31))
// tbl.field1 value = 45450 - WRONG 27456 expected 

这是 Entity Framework 聪明和缓存的一个例子吗?我不明白为什么它会缓存它,因为它已经执行了两次存储过程。

我必须做些什么来关闭tbl吗?

  • 使用 Visual Studio 2008 + Entity Framework。
  • 我也时不时地收到几次“查询不能多次枚举”消息,不确定这是否相关?

完整代码清单

namespace ProfileDataService
{
    public partial class DataService
    {

        public static List<MeterTotalConsumpRecord> GetTotalAllTimesConsumption(DateTime dtStart, DateTime dtEnd, EUtilityGroup ug, int nMeterSelectionType, int nCustomerID, 
                                                 int nUserID, string strSelection, bool bClosedLocations, bool bDisposedLocations)
        {    
            dbChildDataContext db = DBManager.ChildDataConext(nCustomerID);

            var tbl = from t in db.GetTotalConsumptionByMeter(dtStart, dtEnd, (int) ug, nMeterSelectionType, nCustomerID, nUserID, strSelection, bClosedLocations, bDisposedLocations, 1)                       
                      select t;

            return tbl.ToList();            
        }
}
}

/// CALLER

List<MeterTotalConsumpRecord> _P1Totals;
List<MeterTotalConsumpRecord> _P2Totals;

 public void LoadData(int nUserID, int nCustomerID, ELocationSelectionMethod locationSelectionMethod, string strLocations, bool bIncludeClosedLocations, bool bIncludeDisposedLocations,
            DateTime dtStart, DateTime dtEnd, ReportsBusinessLogic.Lists.EPeriodType durMainPeriodType, ReportsBusinessLogic.Lists.EPeriodType durCompareToPeriodType, ReportsBusinessLogic.Lists.EIncreaseReportType rptType,
            bool bIncludeDecreases)
{

   ///Code for setting properties using parameters..        

  _P2Totals = ProfileDataService.DataService.GetTotalAllTimesConsumption(_P2StartDate, _P2EndDate, EUtilityGroup.Electricity, 1, nCustomerID, nUserID, strLocations,
                bIncludeClosedLocations, bIncludeDisposedLocations);

  _P1Totals = ProfileDataService.DataService.GetTotalAllTimesConsumption(_StartDate, _EndDate, EUtilityGroup.Electricity, 1, nCustomerID, nUserID, strLocations, 
                bIncludeClosedLocations, bIncludeDisposedLocations);


  PopulateLines() //This fills up a list of objects with information for my report ready for the totals to be added

  PopulateTotals(_P1Totals, 1);
  PopulateTotals(_P2Totals, 2);

}


 void PopulateTotals(List<MeterTotalConsumpRecord> objTotals, int nPeriod)
 {
        MeterTotalConsumpRecord objMeterConsumption = null;

        foreach (IncreaseReportDataRecord objLine in _Lines)
        {
            objMeterConsumption = objTotals.Find(delegate(MeterTotalConsumpRecord t) { return t.MeterID == objLine.MeterID; });

            if (objMeterConsumption != null)
            {
                if (nPeriod == 1)
                {
                    objLine.P1Consumption = (double)objMeterConsumption.Consumption;
                }
                else
                {
                    objLine.P2Consumption = (double)objMeterConsumption.Consumption;
                }

                objMeterConsumption = null;
            }
        }
    }
}

【问题讨论】:

  • 我已尽力编辑此内容,但老实说,我很难弄清楚这里发生了什么。您可能想发布一个实际工作的代码示例,而不是到处张贴。
  • 感谢您编辑得很好!当我在 2009 年 8 月调用 GetData 时,我得到了我期待的数字 - 45450 当我在 2009 年 7 月调用 GetData 时,我也得到了 45450。这是错误的,我希望得到 27456 我偶尔收到的确切错误消息是 System. InvalidOperationException :查询的结果不能被多次枚举。不知道有没有关系?
  • Paul,这没有用,您只是重复了您已经提供给我们的信息。一方面,GetData 会返回一个 list,而您告诉我们的是单个记录的结果,这没有任何意义。而且这里没有足够的代码来尝试追踪该错误消息的来源 - 当您尝试重用查询结果时会发生这种情况。如前所述,请发布一个重现您的问题的完整代码示例。
  • “db”变量是静态成员作用域变量吗?
  • 我相信是的,刚刚添加了完整的代码清单

标签: linq entity-framework linq-to-entities


【解决方案1】:

考虑将 DataService 类更改为如下所示:

public partial class DataService
{
    public List<MeterTotalConsumpRecord> 
                      GetTotalAllTimesConsumption(SearchCriteria sc)
    {    
        var db = new dbChildDataContext(); // new every time, just in this test case.

        var totalConsumption = db.GetTotalConsumptionByMeter(sc.DtStart, 
                                                             sc.DtEnd, 
                                                             sc.ug, 
                                                             sc.MeterSelectionType, 
                                                             sc.CustomerID, 
                                                             sc.UserID, 
                                                             sc.Selection, 
                                                             sc.ClosedLocations, 
                                                             sc.DisposedLocations, 1)
                                 .ToList();                       

        //inspect how many results are returned.
        int rowCount = totalConsumption.Count;


        return totalConsumption;

    }
} 


//use objects to pass between classes
public class SearchCriteria
{
    public DateTime DtStart {get;set;}
    public DateTime DtEnd {get;set;}
    public int ug {get;set;}
    public int MeterSelectionType {get;set;}
    public int CustomerID {get;set;}
    public int UserID {get;set;}
    public string Selection {get;set;}
    public bool ClosedLocations {get;set;}
    public bool DisposedLocations  {get;set;}
}

【讨论】:

  • 谢谢,这可行,但有没有办法避免创建新的上下文?
猜你喜欢
  • 1970-01-01
  • 2016-06-11
  • 2020-11-05
  • 2021-10-06
  • 2015-10-11
  • 2023-04-05
  • 2021-07-27
  • 1970-01-01
  • 2016-08-13
相关资源
最近更新 更多