简体   繁体   中英

Getting 'The instance of entity type cannot be tracked because another instance with same key value for is already being tracked' with AsNoTracking()

I'm creating my Telegram bot using Entity Framework Core. I have these models:

public class User
{
    public int Id { get; set; }
    public long UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public ICollection<Chat> Chats { get; set;} = new List<Chat>();
    public ICollection<Team> CreatedTeams { get; set; } = new List<Team>();
    public ICollection<Team> Teams { get; set; } = new List<Team>();
}

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? ChatId { get; set; }
    public Chat Chat { get; set; }
    public int? CreatorId { get; set; }
    public User Creator { get; set; }
    public ICollection<User> Users { get; set; } = new List<User>();
}

You don't seem to fully comprehend what AsNoTracking<\/code> does: It explicitly disconnects the entity from the database. So if you want to Update<\/code> that entity, you shouldn't disconnect it in the first place.

If you want to add an existing user<\/code> to a team<\/code> , you need to track the existing object. Else the database will try to create a new one with exactly<\/em> the same values, and there will be a collision.

We are running several large application at my company and almost never have to use Include<\/code> .

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 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked The instance of entity type cannot be tracked because another instance with the same key value 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 entity type cannot be tracked because another instance with the same key value for 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 <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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM