Types Conversion with Fluenti API in EF Core 2.1

0

I wonder if you can do the following conversion:

public class Pessoa 
{
    public bool Ativo { get; set; }
}

public class PessoaMap: IEntityTypeConfiguration<Pessoa>
{
    public virtual void Configure(EntityTypeBuilder<TEntity> builder)
    {
         builder.Property(x => x.Ativo)
            .HasConversion(?) 
         // Converter o meu campo bool em char no banco de dados, por exemplo, quando for true inserir no banco de dados "S", ou inverso "N"
    }
}

    Tabela Pessoa

    | Ativo |
|1| |  'S'  |
|2| |  'N'  |
    
asked by anonymous 02.08.2018 / 18:26

1 answer

0

You need to create the conversion via lambda, the first is the parse of bool to char , second, char to bool (used when loading database)

public class PessoaMap: IEntityTypeConfiguration<Pessoa>
{
    public virtual void Configure(EntityTypeBuilder<TEntity> builder)
    {
         builder.Property(x => x.Ativo)
            .HasConversion(
                x => x.Ativo ? "S" : "N",
                x => x.Equals("S"));
    }
}

There are several pre-defined converters that can be used, including some for your case, the example above is a demonstration of a converter created in the hand, and can serve as an example for other conversions.

Existing converters can be found in the response reference link.

Source: Value Conversions - Microsoft

    
02.08.2018 / 18:47