列表中项目的属性会改变EF源的值 - 有没有办法'分离'它?

时间:2011-08-31 10:40:32

标签: entity-framework list detach

我有一个列表,这个列表正在通过循环填充。每个循环直接从DB获取一个集合。问题是,当它获得CaseNo(PK)时,列表中存在相同的CaseNo并且我更改了其中一个属性的值,那么具有相同CaseNo的项目也已经在列表中也会发生变化。

离。 caseNo 1234,proc code = OTH

然后我再添加一个我从DB获得的项目。但这次我以编程方式更改了proc代码,上面列表中的项也也更改为该值。 我不希望发生这种情况,因为它与记录中的Case不同,因为Case可以将不同类型的专业代码作为其类别。

我知道它正在改变,因为它检测到它与列表中的项目相同,但是我需要在列表中添加相同的案例编号作为单独的项目,因为proc代码是不同的。有没有办法可以将它视为与列表中的实体不同的实体?

实施例。数据的 1.)CaseNO 123 Proc代码OTH 2.)CaseNO 123 Proc代码OTH

但我需要它: 1.)CaseNO 123 Proc代码DSP 1.)CaseNO 123 Proc代码OTH

执行此行时出错。

adrCase.ProcCode = "DSP";

然后列表中的项目(_adrMasterList)如果有任何相同的情况没有,则将其proc代码更改为“DSP”。

这是我的代码:

 private void GenerateReport(string caseCoordinatorID)  //LGF 08012011 ADR-59: add case coordinator - add parameter
    {
        var dispositions = FileMaintenanceBusiness.Instance.GetManyDispositionInfobyKeyword(_selectedProcCode, "ProcCode");
        var adrDispos = FileMaintenanceBusiness.Instance.GetAllADRDispositionInfoList();

        var calendarActivities = FileMaintenanceBusiness.Instance.GetManyCalendarActivityInfobyKeyword(_selectedProcCode, "ProcCode");
        var adrCalActivities = FileMaintenanceBusiness.Instance.GetAllADRCalendars();

        var otherActivities = FileMaintenanceBusiness.Instance.GetManyOtherActivities(_selectedProcCode, "ProcCode");
        var adrOtherActivities = FileMaintenanceBusiness.Instance.GetAllADROtherActivities();
        _adrMasterList.Clear();

        if (_selectedProcCode == "ALL")
        {
            var dispositionALL = FileMaintenanceBusiness.Instance.GetAllDispositionInfoList();
            var adrDisposALL = FileMaintenanceBusiness.Instance.GetAllADRDispositionInfoList();

            var calendarActivitiesALL = FileMaintenanceBusiness.Instance.GetAllCalendarActivityInfoList();
            var adrCalActivitiesALL = FileMaintenanceBusiness.Instance.GetAllADRCalendars();

            var otherActivitiesALL = FileMaintenanceBusiness.Instance.GetAllOtherActivities();
            var adrOtherActivitiesALL = FileMaintenanceBusiness.Instance.GetAllADROtherActivities();

            if (dispositionALL != null && adrDisposALL != null)//dispos
            {
                foreach (var dispo in dispositionALL)
                {
                    foreach (var adrDispo in adrDisposALL)
                    {
                        if (dispo.DispositionID == adrDispo.DispositionID)
                        {
                            var adrCase = FileMaintenanceBusiness.Instance.GetADRMasterInfobyKeyword(adrDispo.CaseNo, "CaseNo");
                            if (adrCase != null)
                            {

                                adrCase.ProcCode = "DSP";
                                adrCase.ProcDateString = dispo.ProcDate.HasValue ? dispo.ProcDate.Value.ToShortDateString() : string.Empty;
                                //LGF 08012011 ADR-59: add case coordinator - add CaseCoordinatorFilter
                                //if (!_adrMasterList.Contains(adrCase) && CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))  
                                //    _adrMasterList.Add(adrCase);
                                if (CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                                    _adrMasterList.Add(adrCase);
                            }
                        }
                    }
                }
            }

            if (calendarActivitiesALL != null && adrCalActivitiesALL != null)//cals
            {
                foreach (var cal in calendarActivitiesALL)
                {
                    foreach (var adrCal in adrCalActivitiesALL)
                    {
                        if (cal.CalendarItemID == adrCal.CalendarItemID)
                        {
                            var adrCase = FileMaintenanceBusiness.Instance.GetADRMasterInfobyKeyword(adrCal.CaseNo, "CaseNo");
                            if (adrCase != null)
                            {
                                adrCase.ProcCode = "CAL";
                                adrCase.ProcDateString = cal.ProcDate.ToShortDateString();
                                //LGF 08012011 ADR-59: add case coordinator - add CaseCoordinatorFilter
                                //if (!_adrMasterList.Contains(adrCase) && CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                                //    _adrMasterList.Add(adrCase);
                                if (CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                                    _adrMasterList.Add(adrCase);
                            }
                        }
                    }
                }
            }
            if (otherActivitiesALL != null && adrOtherActivitiesALL != null)//other activities
            {

                foreach (var otherActivity in otherActivitiesALL)
                {
                    foreach (var adrotherActivity in adrOtherActivitiesALL)
                    {
                        if (otherActivity.ActivityID == adrotherActivity.ActivityID)
                        {
                            var adrCase = FileMaintenanceBusiness.Instance.GetADRMasterInfobyKeyword(adrotherActivity.CaseNo, "CaseNo");
                            if (adrCase != null)
                            {
                                adrCase.ProcCode = otherActivity.ProcCode;
                                adrCase.ProcDateString = otherActivity.ProcDate.ToShortDateString();
                                //LGF 08012011 ADR-59: add case coordinator - add CaseCoordinatorFilter
                                //if (!_adrMasterList.Contains(adrCase) && CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                                if(CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                                    _adrMasterList.Add(adrCase);
                            }
                        }
                    }
                }
            }



        }
        else if (_selectedProcCode == "DSP")
        {
            if (dispositions != null && adrDispos != null)
            {
                foreach (var dispo in dispositions)
                {
                    foreach (var adrDispo in adrDispos)
                    {
                        if (dispo.DispositionID == adrDispo.DispositionID)
                        {
                            var adrCase = FileMaintenanceBusiness.Instance.GetADRMasterInfobyKeyword(adrDispo.CaseNo, "CaseNo");
                            //LGF 08012011 ADR-59: add case coordinator - add CaseCoordinatorFilter
                            if (adrCase != null && CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                            {
                                adrCase.ProcCode = _selectedProcCode;
                                //adrCase.ProcDateString = dispo.ProcDate.ToShortDateString();
                                _adrMasterList.Add(adrCase);
                            }
                        }
                    }
                }
            }
        }
        else if (_selectedProcCode == "CAL")
        {
            if (calendarActivities != null && adrCalActivities != null)
            {
                foreach (var cal in calendarActivities)
                {
                    foreach (var adrCal in adrCalActivities)
                    {
                        if (cal.CalendarItemID == adrCal.CalendarItemID)
                        {
                            var adrCase = FileMaintenanceBusiness.Instance.GetADRMasterInfobyKeyword(adrCal.CaseNo, "CaseNo");
                            //LGF 08012011 ADR-59: add case coordinator - add CaseCoordinatorFilter
                            if (adrCase != null && CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                            {
                                adrCase.ProcCode = _selectedProcCode;
                                adrCase.ProcDateString = cal.ProcDate.ToShortDateString();
                                _adrMasterList.Add(adrCase);
                            }
                        }
                    }
                }
            }
        }
        else
        {
            if (otherActivities != null && adrOtherActivities != null)
            {

                foreach (var otherActivity in otherActivities)
                {
                    foreach (var adrotherActivity in adrOtherActivities)
                    {
                        if (otherActivity.ActivityID == adrotherActivity.ActivityID)
                        {
                            var adrCase = FileMaintenanceBusiness.Instance.GetADRMasterInfobyKeyword(adrotherActivity.CaseNo, "CaseNo");
                            //LGF 08012011 ADR-59: add case coordinator - add CaseCoordinatorFilter
                            if (adrCase != null  && CaseCoordinatorFilter(caseCoordinatorID, adrCase.CaseNo))
                            {
                                adrCase.ProcCode = _selectedProcCode;
                                adrCase.ProcDateString = otherActivity.ProcDate.ToShortDateString();
                                _adrMasterList.Add(adrCase);
                            }
                        }
                    }
                }
            }
        }

        GeneratePrintReport();
    }

1 个答案:

答案 0 :(得分:0)

由于内部identity map pattern实施,这种情况正在发生。您永远不能将具有相同实体键的两个实体实例附加到同一上下文。加载实体后,您只需致电Detach

context.Detach(entity);

或者您可以尝试将实体加载为非跟踪(我不确定它是否解决了这个问题):

context.YourEntitySet.MergeOption = MergeOption.NoTracking;
// Now run the query from YourEntitySet

无论如何,您希望相同实体的两个实例具有不同的值,这很奇怪。您应该为“报告”使用单独的非实体对象,并对该对象进行投影。你根本不需要处理这个问题。