Need explanation, why does EF core require using attach now


This code works fine month or more
this is HttpPost Api endpoint witch add user to user groups

var user = await _userManager.FindByEmailAsync(model.Email.ToUpper()).ConfigureAwait(false);

// if user not finded or not activated return response

var userUserGroups = await _context.UserGroupsUsers
                    .Where(ugu => ugu.User == user)
                    .Select(w => w.UserGroup)

var userGroupsForAdd = promoCodeUserGroups.Where(w => userUserGroups.All(x => x.Id != w.Id)).ToList();

if (!userGroupsForAdd.Any())
   //return response

await _context.UserGroupsUsers
                            w => new UserGroupUser
                                User = user,
                                UserGroup = w

await _context.SaveChangesAsync().ConfigureAwait(false);
//return response all fine

but yesterday started giving an error Cannot insert duplicate key in object 'db.AspNetUsers':

Microsoft.EntityFrameworkCore.Database.Command - Failed executing DbCommand (18ms) [Parameters=[@p0='?' (Size = 450), @p1='?' (DbType = Int32), @p2='?' (Size = 4000), @p3='?' (DbType = DateTime2), @p4='?' (Size = 64), @p5='?' (Size = 4000), @p6='?' (DbType = Boolean), @p7='?' (DbType = Boolean), @p8='?' (DbType = Boolean), @p9='?' (DbType = DateTimeOffset), @p10='?' (Size = 64), @p11='?' (Size = 256), @p12='?' (Size = 256), @p13='?' (Size = 4000), @p14='?' (Size = 4000), @p15='?' (DbType = Boolean), @p16='?' (Size = 4000), @p17='?' (Size = 4000), @p18='?' (DbType = Boolean), @p19='?' (Size = 256)], CommandType='Text', CommandTimeout='30']
INSERT INTO [cat].[AspNetUsers] ([Id], [AccessFailedCount], [ConcurrencyStamp], [CreatedAt], [Email], [EmailConfirmationCode], [EmailConfirmed], [IsActive], [LockoutEnabled], [LockoutEnd], [Name], [NormalizedEmail], [NormalizedUserName], [PasswordHash], [PhoneNumber], [PhoneNumberConfirmed], [ResetPasswordConfirmationCode], [SecurityStamp], [TwoFactorEnabled], [UserName])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19);
System.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_AspNetUsers'. Cannot insert duplicate key in object 'db.AspNetUsers'. The duplicate key value is ({some guid}).

i fixed it.. just using attach for user and usergroups

var isUserInContext = _context.Users.Local.Any(w => w.Id == user.Id);
if (!isUserInContext)

foreach (var userGroup in userGroupsForAdd)
    var isUserGroupInContext 
        = _context.UserGroups.Local.Any(w => w.Id == userGroup.Id);
    if (!isUserGroupInContext)

    var newUserGroupUser 
        = new UserGroupUser
              User = user,
              UserGroup = userGroup

    await _context.UserGroupsUsers.AddAsync(newUserGroupUser);

await _context.SaveChangesAsync().ConfigureAwait(false);

but i can not understand why it's work fine before
server moved from windows to linux
but the error started showing up on my windows machine too

asked on Stack Overflow Oct 23, 2020 by Genusatplay • edited Oct 23, 2020 by Genusatplay

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0