当前位置: 动力学知识库 > 问答 > 编程问答 >

entity framework - Item's property inside a list changes value from EF source - is there a way to 'detach' it?

问题描述:

I have a list and this list is being populated thru loop. Each loop gets a collection directly from DB. The problem is that when it gets CaseNo(PK) that has the same CaseNo existing in the list and I changed the value of one of the property, the item that has the same CaseNo which is already in the list also changes.

ex. caseNo 1234, proc code = OTH

then I add another item which I get from DB again. but this time I programatically change the proc code, the item in the list above also changes to that value also.

I do not want that to happen because it is not the same Case in the record because a Case can have different type of pro code as its category.

I get that it is changing because it is detecting that it is the same item as the one in the list but I need to add the same case number in the list as a separate item because the proc code is different. Is there a way I can treat it as different entity as the one in the list?

Ex. of data

1.)CaseNO 123

Proc code OTH

2.) CaseNO 123

Proc code OTH

but I need it to be:

1.)CaseNO 123

Proc code DSP

1.)CaseNO 123

Proc code OTH

it goes wrong when this line is executed.

adrCase.ProcCode = "DSP";

then the item in the list (_adrMasterList) if there is any with the same case no, changes its proc code too to "DSP".

here is my code:

 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();

}

网友答案:

That is happening because of internal identity map pattern implementation. You can never have attached two entity instances with the same entity key to the same context. You must simply call Detach once you load the entity.

context.Detach(entity);

Or you can try to load entities as non tracked (I'm not sure if it solves this problem):

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

Anyway it is odd that you want two instances of the same entity with different values. You should use separate non entity object for your "report" and make projections to that object. You would not have to deal with this problem at all.

分享给朋友:
您可能感兴趣的文章:
随机阅读: