[英]The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
I have a method that proceses a query and deletes the rows from the table.我有一个处理查询并从表中删除行的方法。
But every time I get the following error: The instance of entity type 'NotificationQueueEntry' 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.
This is the whole method:这是整个方法:
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);
}
The error is in this line _dbContext.Notifications.RemoveRange(query);
错误在这一行
_dbContext.Notifications.RemoveRange(query);
How can I solve this?我该如何解决这个问题? I need to have the
notificationsToProcess
variable as well..我还需要
notificationsToProcess
变量..
have you tried a linq expression to select a list of IDs you can use for items in query, and then maybe delete/save the notifications after your processing is complete您是否尝试过 linq 表达式到 select 可用于查询项目的 ID 列表,然后可能在处理完成后删除/保存通知
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.