Service returns me cast error when consumed

Asked

Viewed 285 times

4

I decided to make another post, since the subject is another, although in the original post, in the comments we touched on the subject, but no depth. I have that mistake:

System.Invalidoperationexception: 'The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not Valid.'

I didn’t understand the error, since in the bank I have the same types as in the application. The difference is that I played in the application the fields float and decimal, I made accept null, as it is in the bank. Below my class Entity Model:

[Table("LIBERACAO")]
    public class Liberacao
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("ID_LIBERACAO")]
        public int IdLiberacao { get; set; }
        [Column("FLAG_LIBERACAO")]
        public byte FlagLiberacao { get; set; }
        [Column("ID_ORCAMENTO")]
        public int IdOrcamento { get; set; }
        [Column("ID_VENDEDOR")]
        public int IdVendedor { get; set; }
        [Column("VENDEDOR")]
        public string Vendedor { get; set; }
        [Column("ID_FILIAL")]
        public int IdFilial { get; set; }
        [Column("FILIAL")]
        public string Filial { get; set; }
        [Column("DATALIB")]
        public float? DataLib { get; set; }
        [Column("HORALIB")]
        public float? HoraLib { get; set; }
        [Column("ID_CLIENTE")]
        public int IdCliente { get; set; }
        [Column("CLIENTE")]
        public string Cliente { get; set; }
        [Column("TIPO_VENDA")]
        public string TipoVenda { get; set; }
        [Column("JUROS")]
        public float? Juros { get; set; }
        [Column("DESCONTO")]
        public float? Desconto { get; set; }
        [Column("VENCIMENTO")]
        public float? Vencimento { get; set; }
        [Column("ACRESCIMO")]
        public float? Acrescimo { get; set; }
        [Column("ENTRADA")]
        public float? Entrada { get; set; }
        [Column("PRAZO")]
        public float? Prazo { get; set; }
        [Column("TOTAL_LIQUIDO")]
        public float? TotalLiquido { get; set; }
        [Column("MIN_TOTAL")]
        public float? MinTotal { get; set; }
        [Column("USUARIO")]
        public string Usuario { get; set; }
        [Column("CUSTODIARIO")]
        public decimal? CustoDiario { get; set; }
        [Column("MAX_COMI")]
        public decimal? MaxComi { get; set; }
        [Column("VALOR_COMI")]
        public decimal? ValorComi { get; set; }
        [Column("NOVA_COMI")]
        public decimal? NovaComi { get; set; }
        [Column("MENSSAGEM")]
        public string Mensagem { get; set; }
        [Column("Menssagem_RET")]
        public string MensagemRet { get; set; }
        [Column("DataRetorno")]
        public double? DataRetorno { get; set; }
        [Column("HoraRetorno")]
        public double? HoraRetorno { get; set; }
        [Column("TempoProcesso")]
        public double? TempoPrecesso { get; set; }
        [Column("Tipo")]
        public int Tipo { get; set; }
        [Column("PROGRAMA")]
        public string Programa { get; set; }
        [Column("NOME_PC")]
        public string NomePc { get; set; }
        [Column("NOME_PROCEDURE")]
        public string NomeProcedure { get; set; }
        [Column("Perc_Juros_Total")]
        public decimal? PercJurosTotal { get; set; }
        [Column("FLAG_CULTURAVENCIDA")]
        public byte FlagCulturaVencida { get; set; }
        [Column("CULTURA")]
        public string Cultura { get; set; }
        [Column("CULTURA_VCTO")]
        public int CulturaVcto { get; set; }
        [Column("FLAG_PRORROGADO")]
        public byte FlagProrrogado { get; set; }
        [Column("VALOR_PRORROGADO")]
        public double? ValorProrrogado { get; set; }
        [Column("DIAS_ATRASO")]
        public int DiasAtrazo { get; set; }
        [Column("ID_VENDEDOR2")]
        public int IdVendedor2 { get; set; }
        [Column("VENDEDOR2")]
        public string Vendedor2 { get; set; }
        [Column("COMISSAO_VEND2")]
        public double? ComissaoVend2 { get; set; }
        [Column("FLAG_COTACAO")]
        public byte FlagCotacao { get; set; }
        [Column("TipoVenda")]
        public string TipoVenda1 { get; set; }
        [Column("Flag_Receber_Atrasado")]
        public byte FlagReceberAtrazado { get; set; }
        [Column("Autorizou_Receber_Atrasado")]
        public string AutorizouReceberAtrazado { get; set; }
    }

Following the guidelines of colleague Gabriel (another post), I created a class Liberacaodto, but only created the class with that name. I don’t know if I should do something else, to understand that it’s not just a name, but a real DTO. I’m not sure if I did it right or not. Down with the class:

public class LiberacaoDTO
    {
        public int IdLiberacao { get; set; }
        public byte FlagLiberacao { get; set; }
        public int IdOrcamento { get; set; }
        public int IdVendedor { get; set; }
        public string Vendedor { get; set; }
        public int IdFilial { get; set; }
        public string Filial { get; set; }
        public float? DataLib { get; set; }
        public float? HoraLib { get; set; }
        public int IdCliente { get; set; }
        public string Cliente { get; set; }
        public string TipoVenda { get; set; }
        public float? Juros { get; set; }
        public float? Desconto { get; set; }
        public float? Vencimento { get; set; } 
        public float? Acrescimo { get; set; }
        public float? Entrada { get; set; }
        public float? Prazo { get; set; }
        public float? TotalLiquido { get; set; }
        public float? MinTotal { get; set; }
        public string Usuario { get; set; }
        public decimal? CustoDiario { get; set; }
        public decimal? MaxComi { get; set; }
        public decimal? ValorComi { get; set; }
        public decimal? NovaComi { get; set; }
        public string Mensagem { get; set; }
        public string MensagemRet { get; set; }
        public double? DataRetorno { get; set; }
        public double? HoraRetorno { get; set; }
        public double? TempoPrecesso { get; set; }
        public int Tipo { get; set; }
        public string Programa { get; set; }
        public string NomePc { get; set; }
        public string NomeProcedure { get; set; }
        public decimal? PercJurosTotal { get; set; }
        public byte FlagCulturaVencida { get; set; }
        public string Cultura { get; set; }
        public int CulturaVcto { get; set; }
        public byte FlagProrrogado { get; set; }
        public double? ValorProrrogado { get; set; }
        public int DiasAtrazo { get; set; }
        public int IdVendedor2 { get; set; }
        public string Vendedor2 { get; set; }
        public double? ComissaoVend2 { get; set; }
        public byte FlagCotacao { get; set; }
        public string TipoVenda1 { get; set; }
        public byte FlagReceberAtrazado { get; set; }
        public string AutorizouReceberAtrazado { get; set; }
    }

The method that populates the DTO and returns a list to the service:

public class PedidoLiberacao
    {
        AutorizadorContext contexto = new AutorizadorContext();
        Liberacao liberacao = new Liberacao();

        public List<LiberacaoDTO> getAutoriza(int idorcamento)
        {
            var lista = contexto.Liberacoes
                        .Where(lib => lib.IdOrcamento == idorcamento)
                        .Select(lib => new LiberacaoDTO
                        {
                            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;
        }
    }

Here my service that picks up the return of the above method:

public class LiberacaoController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        PedidoLiberacao liberacao = new PedidoLiberacao();

        [AcceptVerbs("Get")]
        public IEnumerable<LiberacaoDTO> getLiberacao()
        {
            return liberacao.getAutoriza(1000012093).AsEnumerable().ToList();
            //return contexto.Liberacoes.AsEnumerable().ToList();
         }
    }

Below is my sql server and the database and the fields listed (some for space reasons) inserir a descrição da imagem aqui

The question is: if I’m in the way, why the above mistake?

This is the stack trace of the error

in System.Data.Entity.Core.Common.Internal.Materialization.Shaper.Errorhandlingvaluereader1.GetValue(DbDataReader reader, Int32 ordinal) em lambda_method(Closure , Shaper ) em System.Data.Entity.Core.Common.Internal.Materialization.Coordinator1.Readnextelement(Shaper Shaper) in System.Data.Entity.Core.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext() em System.Data.Entity.Internal.LazyEnumerator1.Movenext() in System.Collections.Generic.List1..ctor(IEnumerable1 Collection)
in System.Linq.Enumerable.Tolist[Tsource](Ienumerable1 source) em AutorizadorService.Controllers.LiberacaoController.getLiberacao() na C:\Projetos\AutorizadorService\AutorizadorService\Controllers\LiberacaoController.cs:linha 22 em lambda_method(Closure , Object , Object[] ) em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 Arguments, Cancellationtoken cancellationToken)

  • 2

    This type of error occurs when comparing incompatible types within a lambda expression. By chance Marking is in fact a int? Would not be a long?

  • 1

    @Edgarmunizberlinck, realment don’t know. In the bank it was declared as int, but its values are great, like: 1000012093. That’s more like long that int, right? That could be one of the examples. That is, going by this logic, the fact that in bd is as int, is not necessarily an int in C#, can be a long and etc...

  • 1

    So your problem is in mapping. Change the int to long and be happy

  • I haven’t been happy yet, the mistake continues. I don’t know, it’s weird. I did a similar service, but in another bank (bank that I created) and works fine, with method returning in the same way. In this one has a field, Datalib, which is a float in the bank (it stores a date) and I have others like this(time) and I still suspect this field. This database I did not create, I just ran the script of these three tables so I could do the tests in "real environment".

  • 1

    The DTO, query creation are correct, its error is still in the assigned values. Note that the error is no longer the same as the old question. Validate all kinds of their properties to ensure that it does not happen the friend commented, from an int receive a very large number.

  • I don’t know what else to do, I’m trying and following your guidelines and still it’s not working. Continues the cast error.

  • That could be one of the problems. This is a Culture problem of its application, certain culture accepts the point as decimal place, while others accept the comma.

  • @Gabrielcoletta and more. There are fields in the table of the real type and how do I represent them in C#? I put as double, but it may be that this is barring aploicação. These REAL types in the bank, are for time and date fields. This will be it?

  • The real of SQL Server is only a float of larger size, everything the float does not accept it will also not accept.

  • I was told that this error may be an Entity bug and there is a tool that fixes this, such a Repair Tool. Get this?

  • I realized that as much as I declare in my model the type float or double or even Double, the Entity is understanding that I have a type Single and when taking the bank the type float, it says that it is not possible to convert. What happens is I don’t know what to do, since it’s an interpretation of the Entity, so I think.

  • 2

    In both DTO and entity Mapping you should use double instead of float.

  • Although the value 1000012093 is large, it is still an int, the int can reach up to 2147483647

Show 9 more comments
No answers

Browser other questions tagged

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