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.
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);
How can I solve this? I need to have the notificationsToProcess
variable as well..
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.