Property error cannot be set to System.Double

Asked

Viewed 137 times

1

I took that mistake:

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

The thing is that I commented on 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 don’t understand why the error persists in the same field. I already gave a clean in Solution and nothing.

  • which line the error occurs ?

  • @Rovannlinhalis, as it is a lambda expression, only at the end of the expression, presents the error, since it reads the expression at once

  • how is the declaration of this field ?

  • [Column("DATALIB")]&#xA; public float DataLib { get; set; }

  • and what type of column in the database ?

Show 1 more comment

2 answers

1

First, you’re giving a .ToList() between the method .Select(lib => new Liberacao) and the method .Where(lib => lib.IdOrcamento == idorcamento), this forces my Iqueryable to run and the . Select() part is no longer SQL manipulation, but a common LINQ.

What happens is your field [Column("DATALIB")] is NULLABLE without value, when you give the Tolist, as I commented, you force the SQL execution by returning a SELECT * 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 accept use Nullable<float> ou float? or set a default value for your DATALIB.

  • When I take the Tolist() before the Select gives me that mistake: The entity or complex type 'Inet.AutorizadorService.Infra.Data.Context.Liberacao' cannot be constructed in a LINQ to Entities query.. This way with Tolist() before the select, I picked up here at Sopt with a post from LINQ

  • You cannot instantiate a class from your context in a query that will be transformed, create a Data Transfer Object called Liberacaomodel for that or something like that. About removing the null from the database, just add Dataannotation [Required] on top of the property. Are you editing the database directly? It is recommended that you use Migrations to make these changes.

  • And how would I do that? Just create a class similar to that, but without assigning BD entities? And how would I link DTO to the main class?

  • Similar, if you don’t need a certain property you don’t need to replicate in the Model, it would look something like: Select(lib => new LiberacaoModel { TipoVenda = lib.TipoVenda }) similar to what you did. To return her to being a Release if necessary, you will need to map her.

  • Just so I’m not understand well. I’ll read something and try. What worries me is that I did another job, in the same way, but with the bank created by me. This was a script that my colleague gave me and gives error in this field, even if I commented on it in the Entity Model.

  • Gabriel, I don’t really understand how to use DTO and Automapper to make it work. Already create my Liberacaodto class and what else I do?

  • Use your DTO to configure which properties you want within your SELECT also you did with Libertacao, with this you will have a Iqueryable<Liberacaodto>. About Automapper it is a package and needs to be configured.

  • Dude, you’re confused, maybe because I’ve never done it. Know any Tuto to help you understand this?

Show 3 more comments

0

I decided as follows: In the DTO class constructor I did this for each double(float field 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 continues float and in the model I put double, because float was giving error, the same cast error reported above. If anyone thinks I’ll have a problem with it, I’m accepting suggestions and corrections.

  • I posted that with float did not work, but now it’s ok. I think it was because of null, although I was not having problems with the ToString() at that moment, but I was giving the stick. I removed the nullable and now it works with float, well, ok and I can map with the same type of the bank this time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.