Use the value of a constant to set a property in the mapping with EF Core

1

I am mapping a class (System) in EF Core, but I would like to create constants for the size of the characters, because when I map the class, I would just change the value of the constant and it would change in all places in the system. I just do not know how to use the constant in my DbContext.cs. Could someone help me?

 public class Sistema
    {
        public const int TamanhoMaxDescricao = 45;

        public int SistemaId { get; set; }
        public string Descricao { get; set; }
        public string Versao { get; set; }

        public virtual ICollection<BackupAgendamento> BackupsAgendamentos { get; set; }
        public virtual ICollection<Tabela> Tabela { get; set; }
        public virtual ICollection<SistemaBackup> SistemaBackups { get; set; }
    }


modelBuilder.Entity<Sistema>()
                .Property(s => s.Descricao)
                .HasColumnName("Descricao")
                .HasColumnType("Varchar(50)") //Usar constante aqui para informar o tanho
                .HasMaxLength(50)
                .IsRequired();
    
asked by anonymous 22.09.2017 / 12:09

1 answer

2

If the variable is already as public const you just have to access it:

Sistema.TamanhoMaxDescricao;

It would look like this:

modelBuilder.Entity<Sistema>()
                .Property(s => s.Descricao)
                .HasColumnName("Descricao")
                .HasColumnType("varchar") 
                .HasMaxLength(Sistema.TamanhoMaxDescricao)
                .IsRequired();

note the HasColumnType that is only "varchar" , the field size is reported in HasMaxLength

    
22.09.2017 / 13:12