![](/img/trans.png)
[英]The instance of entity type <T> cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
[英]The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
我有一個處理查詢並從表中刪除行的方法。
但每次我收到以下錯誤: The instance of entity type 'NotificationQueueEntry' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.
這是整個方法:
public static void ProcessQueue()
{
var query = _dbContext.Notifications
.Where(x => x.TransferInterval == TransferIntervalEnum.Instant);
var notificationsToProcess = query.ToList();
_dbContext.Notifications.RemoveRange(query);
_dbContext.SaveChanges();
if (!notificationsToProcess.Any())
{
return;
}
var queue = notificationsToProcess.Select(notification => new MailRequest
{
Id = notification.Id,
MailType = Smtp.MailTypeEnum.HTML,
MailMessage = new MailMessage
{
content = notification.Content.content,
from = SmtpConfiguration.From, subject = notification.Content.subject,
to = notification.TransferIdentifier
}
}).ToList();
Smtp.ProcessItems(queue, SmtpConfiguration);
}
錯誤在這一行_dbContext.Notifications.RemoveRange(query);
我該如何解決這個問題? 我還需要notificationsToProcess
變量..
您是否嘗試過 linq 表達式到 select 可用於查詢項目的 ID 列表,然后可能在處理完成后刪除/保存通知
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.