Property error can not be set for System.Double

1

I got this error:

  

The 'DataLib' property on 'Release' could not be set to   'System.Double' value. You must set this property to a non-null value   of type 'System.Single'

What happens is that I commented the property in my Entity Model and also in lambda, like this:

public IEnumerable<Liberacao> getAutoriza(int idorcamento)
{
  var lista = contexto.Liberacoes
    .Where(lib => lib.IdOrcamento == idorcamento)
    .ToList()
    .Select(lib => new Liberacao
    {
      TipoVenda = lib.TipoVenda,
      Vencimento = lib.Vencimento,
      Juros = lib.Juros,
      Entrada = lib.Entrada,
      Acrescimo = lib.Acrescimo,
      Desconto = lib.Desconto,
      Mensagem = lib.Mensagem,
      //DataLib = lib.DataLib,
      Vendedor = lib.Vendedor,
      Cliente = lib.Cliente,
      Filial = lib.Filial
    }).ToList();

  return lista;
}

I just do not understand why the error persists in the same field. I already gave a clean in solution and nothing.

    
asked by anonymous 24.08.2017 / 21:53

2 answers

1
First, you are giving .ToList() between the .Select(lib => new Liberacao) method and the .Where(lib => lib.IdOrcamento == idorcamento) method, this forces my IQueryable to be executed and the .Select () part is no longer SQL manipulation, and yes a common LINQ.

What happens is that your [Column("DATALIB")] field is nullable without value, when you give ToList, as I commented, you force SQL execution to return a SELECT * of the database. This null DATALIB value will conflict with your primitive float:

[Column("DATALIB")] public float DataLib { get; set; }

Note that the float field does not accept null, if you want it to use Nullable<float> ou float? or set a default value for your DATALIB.

    
24.08.2017 / 22:25
0

I solved it like this: In the DTO class constructor I did this for every double field (float did not work)

public LiberacaoDTO()
        {
            Mapper.Initialize(cfg =>
            {

                //string userName = null;
                cfg.CreateMap<LiberacaoDTO, Liberacao>()
                    .ForMember(d => d.Juros,
                        opt => opt.MapFrom(src => Juros.ToString("C2")
                        ));

                cfg.CreateMap<LiberacaoDTO, Liberacao>()
                .ForMember(e => e.Entrada,
                opt => opt.MapFrom(src => Entrada.ToString("C2")
                ));
            });
        }

And in the class that has the method I did so, treating the null or 0:

var lista = contexto.Liberacoes
           .Where(m => m.IdOrcamento == idorcamento)
           //.GroupBy(m => m.EmployeeId)
           .Select(m => new LiberacaoDTO
           {
               TipoVenda = m.TipoVenda,
               //Entrada = m.Entrada != 0 ? m.Entrada : 0,
               Juros = m.Juros != 0 ? m.Juros : 0,
               Entrada = m.Entrada != 0 ? m.Entrada : 0,
               MaxComi = m.MaxComi,
               Cliente = m.Cliente,
               Filial = m.Filial
           })
           .ToList();

            return lista;

My concern is that in the database float continues and in the model I put double, because float was giving error, the same cast error reported above. If anyone thinks that I will have problems with this, I am accepting suggestions and corrections.

    
27.08.2017 / 22:47