简体   繁体   English

无法跟踪实体类型“Entity”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例

[英]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.

相关问题 实体类型的实例 <T> 无法跟踪,因为已经跟踪了另一个具有相同的{&#39;Id&#39;}键值的实例 - The instance of entity type <T> cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型 Model 的实例,因为已跟踪另一个具有相同 {&#39;Id&#39;} 键值的实例 - The instance of entity type Model cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“Bus”的实例,因为已经在跟踪具有相同键值 {'Id'} 的另一个实例 - The instance of entity type ‘Bus’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked 无法跟踪实体类型“AppUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'AppUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“IdentityUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'IdentityUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“书签”的实例,因为已经在跟踪具有相同键值 {'ID'} 的另一个实例 - The instance of entity type 'Bookmark' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked 无法跟踪实体类型“Item”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型的实例,因为已跟踪另一个具有与 {'Id'} 相同键值对的实例 - The instance of the entity type cannot be tracked because another instance with the same key value pair for{'Id'} is already being tracked 无法跟踪实体类型“ Article”的实例,因为已经跟踪了另一个具有相同键值的{&#39;Id&#39;}实例 - The instance of entity type 'Article' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“用户”的实例,因为已经在跟踪另一个具有相同键值的“Id”实例 - 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