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.