C# 错误:";实体类型的实例';采购订单';无法跟踪,因为另一个实例的';道具';已经被跟踪了
当下面的代码运行时,我得到了上面的错误。我做错了什么C# 错误:";实体类型的实例';采购订单';无法跟踪,因为另一个实例的';道具';已经被跟踪了,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,当下面的代码运行时,我得到了上面的错误。我做错了什么 var collectionReport = await _collectionReportRepository.GetAll().FirstOrDefaultAsync(c => c.PaymentRefNumber == split.PaymentReferenceNumber); if (collectionReport != null) { collectionReport.IsSplitted = true;
var collectionReport = await _collectionReportRepository.GetAll().FirstOrDefaultAsync(c => c.PaymentRefNumber == split.PaymentReferenceNumber);
if (collectionReport != null)
{
collectionReport.IsSplitted = true;
await _collectionReportRepository.UpdateAsync(collectionReport);
//Create the child collections in CollectionReportsTable
// var splitCollections = await GetSplitCollectionReportsByPayRef(collectionReport.PaymentRefNumber);
var splitCollections = await GetSplitCollectionReportsByPayRef(split.PaymentReferenceNumber);
var childCollections = SplitExtensions.FromSplitCollectionsToCollections(splitCollections, collectionReport);
await _collectionReportRepository.SaveRangeAsync(childCollections);
}
上面调用的方法如下所示
public async Task<List<SplitCollectionReport>> GetSplitCollectionReportsByPayRef(string payRef) => await _splitCollectionReportRepository.GetAll().Where(c => c.MasterPaymentReferenceNumber == payRef).AsNoTracking().ToListAsync();
public异步任务GetSplitCollectionReportsByPayRef(string payRef)=>Wait_splitCollectionReportRepository.GetAll()。其中(c=>c.MasterPaymentReferenceNumber==payRef)。AsNoTracking().ToListSync();
public async Task SaveRangeAsync(列表范围)
{
wait context.Set().addRangeSync(范围);
var saveCount=await context.SaveChangesAsync();
返回saveCount;
}
我不知道为什么它给了我下面的错误
无法跟踪实体类型“CollectionReport”的实例,因为已在跟踪另一个具有{'PaymentRefNumber'}相同键值的实例。附着现有实体时,请确保仅附着一个具有给定键值的实体实例。考虑使用“dBraveTopStudioBuffel.EnabelSythViDeAtCug”来查看冲突的键值。
为什么标题中的错误与问题正文中的错误不同?消息的字面意思是
{'PaymentRefNumber'}
还是您编辑了实际值?标题不能包含所有字符串我问它为什么不同,而不是更短。
public async Task<int> SaveRangeAsync(List<T> range)
{
await context.Set<T>().AddRangeAsync(range);
var saveCount = await context.SaveChangesAsync();
return saveCount;
}