Hi,
I’m not sure if this is the intended behavior or not.
If an enum is set to nullable on a DTO and I use Db.UpdateNonDefaultsAsync
, providing the first enum value does not update the database. It seems the first enum value is considered the default, even when the enum is nullable.
Here’s a minimal reproducible example:
// Migration1000
private class Tenant
{
[AutoIncrement]
public long? Id { get; set; }
[Required]
public TenantStatus? TenantStatus { get; set; }
}
// In ServiceModel
public class Tenant
{
[AutoIncrement]
public long? Id { get; set; }
[Required]
[ValidateNotNull]
public TenantStatus? TenantStatus { get; set; }
}
public enum TenantStatus
{
Active,
Archived,
Disabled,
Unknown,
}
[Route("/tenant/{Id}", "PATCH")]
[ValidateHasRole("Admin")]
public class UpdateTenant : IPatchDb<Tenant>, IReturn<IdResponse>
{
[ValidateGreaterThan(0)]
public long Id { get; set; }
public TenantStatus? TenantStatus { get; set; }
}
// In ServiceInterface
public async Task<object> PatchAsync(UpdateTenant req)
{
// here req.TenantStatus == TenantStatus.Active
var tenant = await Db.SingleByIdAsync<Tenant>(req.Id);
tenant.TenantStatus = req.TenantStatus;
await Db.UpdateNonDefaultsAsync(tenant, x => x.Id == req.Id);
// Database is not updated.
}
When req.TenantStatus
is set to TenantStatus.Active
, the Db.UpdateNonDefaultsAsync
method does not update the database, which seems to treat TenantStatus.Active
as a default value.
Is this the intended behavior?
For the time being I’ve added a Enum TenantStatus.Undefined
as the first value.
Thanks
PS. I’m using Postgresql.