Page 1 of 1

DbUpdateConcurrencyException - wrong SQL generated

Posted: Mon 25 May 2020 16:54
by Adaƛ
Dont know why but this simple code:

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!");
 }
generates DbUpdateConcurrencyException. Checked debug and noticed that instead of INSERT there is called update:

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;
Any idea how to fix it?

Re: DbUpdateConcurrencyException - wrong SQL generated

Posted: Fri 05 Jun 2020 20:56
by Shalex
Please localize the issue in a small test project and send it with the corresponding DDL/DML script to us via contact form.