DbUpdateConcurrencyException - wrong SQL generated
Posted: Mon 25 May 2020 16:54
Dont know why but this simple code:
generates DbUpdateConcurrencyException. Checked debug and noticed that instead of INSERT there is called update:
Any idea how to fix it?
Code: Select all
Guild guild = _context.Guilds.Where(w => w.GuildId == guildId).SingleOrDefault();
if (guild != null)
{
GuildMemberRank guildMemberRank = (guild.GuildMembers.Count() == 0 ? GuildMemberRank.GuildMaster : GuildMemberRank.Member);
guild.GuildMembers.Add(new GuildMember() { GuildRank = guildMemberRank, JoinDate = DateTime.Now, UserId = userId });
_context.SaveChanges();
}
else
{
throw new AppException("Guild not found!");
}
Code: Select all
Executed DbCommand (5ms) [Parameters=[p3='?' (DbType = Int32), p0='?' (DbType = Int32), p1='?' (DbType = Int32), p2='?' (DbType = DateTime)], CommandType='Text', CommandTimeout='30']
UPDATE GuildMembers SET GuildId = :p0, GuildRank = :p1, JoinDate = :p2 WHERE UserId = :p3;