简体   繁体   中英

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.

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.

Related Question 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 Model cannot be tracked because another instance with the same key value for {'Id'} is already being tracked The instance of entity type ‘Bus’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked The instance of entity type 'AppUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked The instance of entity type 'IdentityUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked The instance of entity type 'Bookmark' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked The instance of the entity type cannot be tracked because another instance with the same key value pair for{'Id'} is already being tracked The instance of entity type 'Article' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked The instance of entity type 'User' cannot be tracked because another instance with the same key value for 'Id' is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM