I'm connecting with EF Core to the existing PostgreSQL database. There is a non-nullable flag column IsAvailable
defined as bit(1)
. Scaffolded entity ends up with BitArray IsAvailable
property where I expected it to be a bool
. I tried changing the type manually but I end up with an exception when I query the data.
The property 'Loan.IsAvailable' is of type 'bool' which is not supported by the current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
I also tried playing around with the private field, [NotMapped]
property, and [Column("is_available")]
attribute on the field, but that was not working either. Has anyone encountered this issue before? What can I do about it?
Column definition in entity configuration
entity.Property(e => e.IsAvailable)
.IsRequired()
.HasColumnType("bit(1)")
.HasColumnName("is_available");
ValueConverters
were what I needed, I stumbled upon them in the official docs.
Ended up creating one of my own for this type of conversion.
Converter definition
public static class ConverterProvider
{
public static ValueConverter<bool, BitArray> GetBoolToBitArrayConverter()
{
return new ValueConverter<bool, BitArray>(
value => new BitArray(new[] { value }),
value => value.Get(0));
}
}
Column mapping
entity.Property(e => e.IsCurrent)
.IsRequired()
.HasColumnType("bit(1)")
.HasColumnName("is_current")
.HasConversion(ConverterProvider.GetBoolToBitArrayConverter());
And I swapped the property to bool IsAvailable
within my entity manually.