Untitled
unknown
csharp
2 years ago
2.7 kB
5
Indexable
public List<User> ConcurrencyConflictMethodButSaved([Service(ServiceKind.Resolver)] WorkoutsContext context, Guid _userId) { var user = context.Users.Find(_userId); user.Username = "Wassup"; var newVersion = Guid.NewGuid(); var sql = $"UPDATE \"Users\" SET \"Version\" = '{newVersion}' WHERE \"UserId\" = '{_userId}' AND \"Version\" = '{user.Version}'"; string error = ""; try { context.Database.ExecuteSqlRaw(sql); context.Entry(user).Property(u => u.Version).CurrentValue = newVersion; context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is User) { var proposedValues = entry.CurrentValues; var databaseValues = entry.GetDatabaseValues(); foreach (var property in proposedValues.Properties) { var proposedValue = proposedValues["Version"]; var databaseValue = databaseValues[property]; if (databaseValue != proposedValue) { throw new GraphQLException( ErrorBuilder .New() .SetMessage($"User modified since read: Old version {databaseValue} - New version: {proposedValue}") .SetCode("CON") .SetExtension("Concurrent", "Db") .Build()); // error = "The user has been modified by another user."; // break; // Exit the loop since the error is already determined } if (databaseValues == null) { error = "The user has been deleted by another user."; break; // Exit the loop since the error is already determined } } } } if (string.IsNullOrEmpty(error)) { error = "Concurrency conflict occured, unable to determine type"; } throw new DbUpdateConcurrencyException(error); } return new List<User>(); }
Editor is loading...