I use generic method to update entity data
// In repository implementation
public async Task Update<T>(int id, object model) where T: IEntity // (with Id)
{
_dbContext.Attach(new T { Id = id }).CurrentValues.SetValues(model);
await _dbContext.SaveChangesAsync();
}
I found a case in which data is not completely updated. I will give an example.
Simple db entity:
class MyEntity : IEntity
{
public int Id {get; set;}
public string Name {get; set;}
public bool Active {get; set;}
public Guid EditorGuid {get; set;}
public DateTime Modified {get; set;}
}
And my simple business logic method:
public async Task ChangeActive(int id, bool active)
{
var userGuid = _userService.GetCurrent().Guid;
await _repository.Update<MyEntity>(id, new
{
EditorGuid = userGuid,
Active = active,
Modified = DateTime.UtcNot
});
}
All queries to the database are logged into the console. When executing the ChangeActive method, I see the following SQL query:
UPDATE public.my_entity
SET editor_guid = @p0, modified = @p1
WHERE id = @p2;
SQL is generated in such a way that the Active field is not updated. I want to note that in other cases that use the Update method in my code, this problem does not occur.
What could be the problem?
UPDATE
If I explicitly specify a value, then the SQL query is generated correctly
await _repository.Update<MyEntity>(id, new
{
EditorGuid = userGuid,
Active = true, // <--
Modified = DateTime.UtcNot
});
UPDATE public.my_entity
SET active = @p0, editor_guid = @p1, modified = @p2
WHERE id = @p3;