DbConcurrencyException, what am I missing?
Hi, getting this on the SaveChangesAsync and im not really sure what im doing wrong.
DbConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded.
Inside UserService.cs:
Inside User.cs:
Inside UserRepository.cs:
DbConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded.
Inside UserService.cs:
public async Task SetVariable(Guid productId, Guid userId, string requestKey, string requestValue)
{
var product = await productRepository.GetByIdAsync(productId);
if (product is null)
{
throw new ProductNotFoundException(productId);
}
var user = await userRepository.GetByIdAsync(productId, userId);
if (user is null)
{
throw new UserNotFoundException(userId);
}
user.SetVariable(requestKey, requestValue);
userRepository.Update(user);
await unitOfWork.SaveChangesAsync();
} public async Task SetVariable(Guid productId, Guid userId, string requestKey, string requestValue)
{
var product = await productRepository.GetByIdAsync(productId);
if (product is null)
{
throw new ProductNotFoundException(productId);
}
var user = await userRepository.GetByIdAsync(productId, userId);
if (user is null)
{
throw new UserNotFoundException(userId);
}
user.SetVariable(requestKey, requestValue);
userRepository.Update(user);
await unitOfWork.SaveChangesAsync();
}Inside User.cs:
public ICollection<UserVariable> Variables { get; set; }
public void SetVariable(string key, string value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key);
var existingVariable = Variables.FirstOrDefault(v => v.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
if (existingVariable != null)
{
existingVariable.UpdateValue(value);
}
else
{
var newVariable = UserVariable.Create(Guid.NewGuid(), Id, key, value);
Variables.Add(newVariable);
}
} public ICollection<UserVariable> Variables { get; set; }
public void SetVariable(string key, string value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key);
var existingVariable = Variables.FirstOrDefault(v => v.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
if (existingVariable != null)
{
existingVariable.UpdateValue(value);
}
else
{
var newVariable = UserVariable.Create(Guid.NewGuid(), Id, key, value);
Variables.Add(newVariable);
}
}Inside UserRepository.cs:
public void Update(User user)
{
dbContext.Set<User>().Update(user);
} public void Update(User user)
{
dbContext.Set<User>().Update(user);
}