Преобразовать bool в bit. EF Core. PostgreSQL

Рейтинг: 0Ответов: 1Опубликовано: 20.07.2023

Есть сущность, у которой свойство имеет тип данных в C# bool. Например

    public class Entity
    {
        
        public int Id { get; set; }

        public bool Flag{ get; set; }
    }

Можно ли преобразовать bool в bit средствами EF Core для БД Postges?

Пробовал так

modelBuilder.Entity<Entity>()
           .Property(x => x.Flag)
           .IsRequired()
           .HasColumnType("bit");

Но при создании миграции получаю ошибку:

The property 'Entity.Flag' 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'.

Ответы

▲ 0

Есть вариант с анатацией полей:

using System.ComponentModel.DataAnnotations.Schema;
public class MyEntity
{
    // Преобразование bool в bit
    [Column(TypeName = "bit")]
    public bool MyProperty { get; set; }
}