【Bug】The instance of entity type ‘xxx‘ cannot be tracked because another i
文章目录
问题
The instance of entity type ‘ReportMonthlyOrWeeklyPassRate’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using ‘DbContextOptionsBuilder.EnableSensitiveDataLogging’ to see the conflicting key values.
问题代码
具体是在报在了ReportMonthlyOrWeeklyPassRateRepository.UpdateRange(statisticsMonthsUpdate);
List<ReportMonthlyOrWeeklyPassRate> statisticsMonthsUpdate=new List<ReportMonthlyOrWeeklyPassRate>();
List<ReportMonthlyOrWeeklyPassRate> statisticsMonths_DB = ReportMonthlyOrWeeklyPassRateRepository.FindAsIQueryable(f => statisticsMonths.Select(f=>f.year).ToList().Contains(f.Year)&&statisticsMonths.Select(f => f.month).ToList().Contains(f.MonthOrWeek) && f.Type == 1).AsNoTracking().ToList();
foreach (var item in statisticsMonths)
{
if (item.year == 1900)//新增但是未判定的不能计算
{
continue;
}
var statisticsMonth = statisticsMonths_DB.FirstOrDefault(f=>f.Year==item.year&&f.MonthOrWeek==item.month && f.Type == 1);
//不存在
if (statisticsMonth==null)
{
ReportMonthlyOrWeeklyPassRate reportMonthlyOrWeeklyPassRate = new ReportMonthlyOrWeeklyPassRate
{
Type = 1,
MonthOrWeek = item.month,
Year = item.year,
YearMonth= new DateTime(item.year, item.month,15),
QualifiedBatchesNum = item.qualified,
UnqualifiedBatchesNum = item.unqualified,
BatchesNumCount = item.qualified + item.unqualified,
FirstPassRate = (item.qualified + item.unqualified) == 0 ? "0%" : (item.qualified / (item.qualified + item.unqualified).ToDecimal()*100).ToString("f2") + "%",
};
reportMonthlyOrWeeklyPassRate.SetCreateDefaultVal();
ReportMonthlyOrWeeklyPassRateRepository.Add(reportMonthlyOrWeeklyPassRate);
}
//存在
else
{
//已经更新的延用更新的数据
var statisticsMonth_ = statisticsMonthsUpdate.FirstOrDefault(f => f.Year == item.year && f.MonthOrWeek == item.month && f.Type == 1);
if (statisticsMonth_!=null)
{
statisticsMonth = statisticsMonth_;
}
else
{
statisticsMonthsUpdate.Add(statisticsMonth);
}
statisticsMonth.QualifiedBatchesNum += item.qualified;
statisticsMonth.UnqualifiedBatchesNum += item.unqualified;
statisticsMonth.BatchesNumCount = statisticsMonth.QualifiedBatchesNum + statisticsMonth.UnqualifiedBatchesNum;
statisticsMonth.FirstPassRate = statisticsMonth.BatchesNumCount == 0 ? "0%" : (statisticsMonth.QualifiedBatchesNum / statisticsMonth.BatchesNumCount.ToDecimal() * 100).ToString("f2") + "%";
statisticsMonth.SetModifyDefaultVal();
}
}
if (statisticsMonthsUpdate.Count>0)
{
ReportMonthlyOrWeeklyPassRateRepository.UpdateRange(statisticsMonthsUpdate);
}
原因
外层数据分批次调用,虽然savechange了但是还保留着对同一entity的跟踪
for (int i = 1; i <= maxIndex; i++)
{
var pageDataOptions = new PageDataOptions
{
PageIndex = i,
PageSize = oneQueryCount,
Wheres = searchParameters,
Orders = new List<OrderModel> { new OrderModel{
Sort="OutDataId",
Order=QueryOrderBy.Asc.ToString(),
}}
};
var response = await DataCenterService.GetBizSyncCheckNoticePageData(pageDataOptions);
if (response.rows.IsEmpty())
throw new Exception($@"查询报错,入参:{pageDataOptions.Serialize()}");
string d = response.rows.Serialize();
mesSyncFromErpBizQcllInputEvent.Data = new MesSyncFromErpBizQcllInputEventData
{
InsertBizQcBillOutputList = new List<ApiDtosBizQcBillOutput>(),
UpdateBizQcBillOutputList = response.rows
};
webResponseContent = Service.StatisticsReportMonthlyOrWeeklyPassRate(mesSyncFromErpBizQcllInputEvent);
}
解决
取消分批调用。

此类问题相关解决方案:
解决方案1:查询的时候使用:AsNoTracking(),此时是不带变动跟踪的实体查询。
解决方案2:更新数据时,我们手动去trace一次:EntityState.Detached。
处理Bug的具体步骤
- 重现BUG
- 确定发生BUG的过程时都做了哪些动作
- 桌面检查:把自己的脑子作为CPU,运行从开始操作到出现BUG的操作、处理的数据,分析出现问题的原因
- 调试:确定问题出现的原因
- 确定解决方案
- 修复BUG
- 回归测试:确定已BUG已修复并没引入新问题
没有粉丝,没有评论,甚至连读者都没有。因为自己写的烂,因为自己水平有限,所以自己的作品一经发出就石沉大海了,得不到任何的回信,就好像自己写的东西是给自己看的。自己的作品怎么看都好像不够完美,全身的毛病,所以读者不喜欢也很正常对吧!
其实最让我开心的是有些读者们夸我文笔不错,或许是有人第一次这样夸我的缘故吧!又或许是我写的东西没什么人认可,我感受到了一种前所未有的激动,对于未来的憧憬,对自己写下去的动力。
因为有你们,我的写作之路才不孤独,因为有你们,我才能坚持到现在,如果有一天我真的写出圈了,或许我们可以整个交流会之类的,到时候促膝长谈。
“书山有路勤为径,学海无涯苦作舟。”写作苦是苦了点,整得我差点就想放弃了,不过还好有你们。我也不急着成功,因为没有个几十年如一日的坚持,哪能轻易成功?
茫茫文海无人意,天下有君识吾心。文海很孤独,但是有诸君,便足以令我至千里。
更多推荐
所有评论(0)