![](/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.