When consuming a service, error occurs in fields float or double in the BD

2

When I use the update service, this error occurs:

  

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'

This error happens when

[Route("{id}/{value}")]
        public void AtualizaLiberacao(int id, string value)
        {
            var lista = contexto.Liberacoes
                        .Where(l => l.IdOrcamento == id)
                        .ToList();

            lista.ForEach(f =>
            {
                f.FlagLiberacao = 0;
                f.AutorizouReceberAtrazado = value;
            });
            contexto.SaveChanges();
        }

I've already changed the Model field to: double, float, single, and decimal , and still the problem continues. As if Entity or Lambda did not recognize the type. I know a lambda expression does fetch all of the records. See, I just want to update two fields, a string and another byte. Can you make a lambda to bring only the fields in question? I tried to give a select and it did not work, like:

var lista = contexto.Liberacoes
                    .Where(l => l.IdOrcamento == id)
                    //.ToList()
                    .Select(s => new Liberacao
                    {
                        FlagLiberacao = s.FlagLiberacao,
                        AutorizouReceberAtrazado = s.AutorizouReceberAtrazado
                    }).ToList();

When I do the above, it gives this error:

  

The entity or complex type   'Inet.AuthorizerService.Infra.Data.Context.Liberation' can not be   constructed in a LINQ to Entities query.

As I said, I've tried to change the type and it still does not work. I have no more resources.

EDIT1

In the get service I passed in the DTO everything to string and I was able to resolve this error. Well, I tried to do it the same way, as below:

[Route("{id}/{value}")]
        public void AtualizaLiberacao(int id, string value)
        {
            var lista = contexto.Liberacoes
                        .Where(l => l.IdOrcamento == id)
                        .Select(libera => new LiberacaoDTO
                        {
                            TipoVenda = libera.TipoVenda,
                            IdOrcamento = libera.IdOrcamento,
                            Juros = libera.Juros.ToString(),
                            Entrada = libera.Entrada.ToString(),
                            Acrescimo = libera.Acrescimo.ToString(),
                            Desconto = libera.Desconto.ToString(),
                            Mensagem = libera.Mensagem,
                            DataLib = libera.DataLib.ToString(),
                            Vencimento = libera.Vencimento.ToString(),
                            Vendedor = libera.Vendedor,
                            Cliente = libera.Cliente,
                            Filial = libera.Filial
                        })
                        .ToList();
            lista.ForEach(f =>
            {
                f.FlagLiberacao = 0;
                f.AutorizouReceberAtrazado = "Testando";
            });
            contexto.SaveChanges();
        }

Well, the error has disappeared, but I have two problems with this. You have not updated and the value parameter that I'm passing in URL is coming null . As the list is Liberation and not DTO, I thought it could be done that way. I'm waiting. I passed the AuthorizedReceiveAtrazado value as it can be seen as a literal and still did not update. If anyone can give me that strength, I'll wait.

EDIT2

I've been missing the service.

[RoutePrefix("api/Atualiza")]
    public class AtualizaController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        PedidoLiberacao liberacao = new PedidoLiberacao();

        [Route("{id}/{value}")]
        [AcceptVerbs("Put")]
        public void putItensLiberacao(int id, string value)
        {
            liberacao.AtualizaLiberacao(id, value);
        }
    }

In the string value parameter, there was a [FromBody] , I removed the parameter and is now coming with the correct value but does not update.

EDIT3

I tried to do this:

var lib = new Liberacao();
            lib.AutorizouReceberAtrazado = value;
            lib.FlagLiberacao = 0;
            contexto.Entry(lib).State = EntityState.Modified;

            contexto.SaveChanges();

But when I save, it gives this error:

  

Store update, insert, or delete statement   number of rows (0). Entities may have been modified or deleted since   groups were loaded. See    link for information on   understanding and handling optimistic concurrency exceptions.

Now I'm going to do as colleague Gabriel Colleta said

EDIT 4

List<LiberacaoDTO> lista = new List<LiberacaoDTO>();

            var lib = lista
                .Where(dto => dto.IdOrcamento == id)
                .Select(libera => new Liberacao
                {
                    TipoVenda = libera.TipoVenda,
                    IdOrcamento = libera.IdOrcamento,
                    Juros = float.Parse(libera.Juros),
                    Entrada = float.Parse(libera.Entrada),
                    Acrescimo = float.Parse(libera.Acrescimo),
                    Desconto = float.Parse(libera.Desconto),
                    Mensagem = libera.Mensagem,
                    DataLib = float.Parse(libera.DataLib),
                    Vencimento = float.Parse(libera.Vencimento),
                    Vendedor = libera.Vendedor,
                    Cliente = libera.Cliente,
                    Filial = libera.Filial,
                    FlagLiberacao = libera.FlagLiberacao,
                    AutorizouReceberAtrazado = libera.AutorizouReceberAtrazado
                }).ToList();

            lib.ForEach(l => 
            {
                l.AutorizouReceberAtrazado = value;
                l.FlagLiberacao = 0;
            });

            contexto.SaveChanges();
    
asked by anonymous 14.09.2017 / 20:45

2 answers

4

Simple, its DbSet is of type Liberacao and not LiberacaoDTO , so the Entity Framework does not understand the changes. When you do this new ReleaseDTO, it's a brand new object that the Entity Framework no longer has tracking (and consequently, its edits).

@Edit: Your biggest problem is the inconsistency between mapping your domain and your database. If your database has a column that is NULLABLE, your entity representing it in the backend should also be to avoid these "System.Double ..." issues.

This code would solve if you had this mapping correct:

var lib = lista
    .Where(dto => dto.IdOrcamento == id).ToList();

foreach (var item in lib)
{
    item.AutorizouReceberAtrazado = item;
    item.FlagLiberacao = 0;
}

contexto.SaveChanges();

It resolves because when I do not Enumerable.Select() , I'm getting a IQueryable<Liberacao> , which is exactly what the Entity Framework knows and can do tracking (Even if you later call Enumerable.ToList , the Entity maintains tracking by reference). In this case, the Entity Framework itself would understand that the object was changed in the ObjectContext.SaveChanges() call.

In your case if you are breaking when you call everyone, the best thing to do is to take some time and fix this inconsistency.

    
14.09.2017 / 22:52
1

RESOLVI !!

Personal, what I've done: I've re-designed the project, in fact just a service on VS2015 . So I managed to solve it, but I still think VS2017 will work, because I did something in vs VS2015 that I did not do that in VS2017 . He ended up giving another error that had nothing to do with the float or double question. I saw what the error was, corrected it and went up. When I corrected it I noticed that it gave the same error and I had changed where the error applied, then I gave a clean solution and rebuilt each project (3) and went up and did not give the error any more. I changed the fields to double and not float , after a lot, I understood float in BD (8b) does not match float in .Net (4b) . When I uploaded, the error in the DataLib field disappeared, but appeared in this DataRetorno field, which in the database is of type REAL strong> and reading, I saw that it is represented in .NET as Single. I changed and the error persisted. I again gave a clean and appeared in another field and so I did, changing and cleaning, until by the service, I was able to give the update . I do not consider mine the solution. I think the colleague Gabriel Colleta deserves to receive the point, because it was through his tips that I came to the solution. At the time of this post, I had not done all of this in VS2017 , that is, clearing the solution. If the VS2017 error persists, I will do my services for VS2015 and App Xamarin for VS2017 . Thanks everyone. I spent the whole Sunday from 6:00 in the morning and I'm going to stall the dawn to resolve the issue of notifications in Xamarin, but that's another post.

    
17.09.2017 / 22:06