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

Asked

Viewed 122 times

2

When consuming the update service, this error occurs:

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'

This mistake 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 Model’s field to: double, float, Single and decimal And yet, the problem remains. As if the Entity or Lambda didn’t recognize the type. I know a lambda expression, it fetch all the records. Check it out, I just want to update two fields, a string and another byte. Can you do a lambda, to bring only the fields in question? I tried to give a select and it didn’t 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 it the way above, it makes that mistake:

The Entity or Complex type 'Inet.AuthorizerService.Infra.Data.Context.Release' cannot be constructed in a LINQ to Entities query.

As I said, I tried changing the type and it still doesn’t work. I don’t have more resources.

EDIT1

In the get service I passed in DTO all to string and managed to fix this error. Well, I tried to do the same 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 disappeared, but I have two problems with it. It did not update and the value parameter I pass in URL is coming null. As list is Release and not DTO, I thought it could be done so. I stand by. I passed the value of Authorize as can be seen, as a literal and yet not updated. If someone can give me that strength, I wait.

EDIT2

I missed posting 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], removed the parameter and is now coming with the correct value, but does not update.

EDIT3

I tried to do so:

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

            contexto.SaveChanges();

But when I save, it makes that mistake:

Store update, Insert, or delete statement affected an Unexpected number of Rows (0). Entities may have been modified or Deleted Since entities Were Loaded. See http://go.microsoft.com/fwlink/? Linkid=472540 for information on understanding and Handling optimistic concurrency exceptions.

Now I will do as 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();
  • When a new of the Liberation class is made, the object is created as a whole, even if all properties are not initialized. Tip: Initialize properties that cannot be null in the constructor

  • @Alexandrecavaloti, well, the fields float, double I’ll start in the class constructor with 0, that’s right?

  • Yes, I imagine that, do the test after that change and if that’s what we formalize the solution.

  • @Alexandrecavaloti, nothing expensive, did not work. Continues the error. I saw that in the Model these fields already have an attribute to start and even so I started the constructor with these values.

  • @Alexandrecavaloti, I made an edition of a new form that I found. If you can give me a help, I thank you.

2 answers

4


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

@Edit: Your biggest problem is the inconsistency between your domain mapping and your database. If in your bank you have a column that is NULLABLE, your entity that represents you in the back end should also be to avoid these problems of "System.Double ...".

This code would solve if it had this correct mapping:

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

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

contexto.SaveChanges();

It solves because when I don’t make one Enumerable.Select(), I’m getting a IQueryable<Liberacao>, which is exactly what the Entity Framework has knowledge of and can track (even if you call the Enumerable.ToList, entity keeps tracking by reference). In this case, the Entity Framework itself would understand that the object was changed in the ObjectContext.SaveChanges().

In your case if you’re breaking when you call everyone, the best thing to do is to take a break and fix this inconsistency.

  • turn the DTO back into the entity. Your biggest problem is being to do what the stackoverflow people say without really understanding what is what. This float problem is not null even you have already opened some 3 similar topics. I strongly recommend that you stop and study heavy on the subject

  • I can’t stop. Unfortunately this is my livelihood. I know you’re right, but the rush is too big. I can’t stop. I don’t disagree with any of you, but it’s my reality, unfortunately. Still, I really appreciate.

  • Let’s help regardless, it was just a friend tip. Here did not appear the Edit 4

  • Thanks for the help and as I said, you are right. I have read a lot, especially Xamarin. It is a lot to learn and little time. I am a gentleman and I need to produce. Many mouths and I know it is not easy, but I am learning a lot here and on other sites/ forums. I edited the post

  • I don’t understand why you’re not updating. I’m trying to use the Entitystate but I couldn’t make it work.

  • Entitystate will only work in an entity registered in Dbset, in your case Release.

  • This has nothing to do with Sql Server? Is it? Some kind of extra configuration, I don’t know, something external to the bank? Is that possible?

  • If in your bank you have a column that is NULLABLE, your entity that represents you in the back end should also be to avoid these problems of "System.Double ...". I’ve done it several times and at that moment I set out to float? and the error persists. In the bank is nullable float

Show 4 more comments

1

I resolved!!

Guys, what I did: I remade the project, in reality just a service on VS2015. That way, I was able to solve it, but I still think VS2017 it will work because I did something in vs VS2015 that I didn’t do that way at VS2017. It ended up making another mistake that had nothing to do with the question float or double. I saw what the error was, corrected it and went up. When I had corrected I realized that it gave the same error and I had changed where the error applied, so I gave a clean in Solution and rebuilt each project(3) and went up and did not give more error. I changed the fields to double and not float, after a lot of things, I understood that float in the BD(8b) does not correspond to the float in the .Net(4b). When I went up, the mistake in the field Datalib disappeared, but appeared in this field Return date, that in the bank is of the type REAL and reading, I saw that he is represented in .NET as Single. I changed and the error persisted. I gave again a clean and appeared in another field and so I was doing, changing and cleaning, until by the service, I was able to give the update. I do not consider mine, the solution found. I think the colleague Gabriel Colleta is worth receiving the point, because it was for his tips, I arrived at the solution. At the time of this post, VS2017, that is to say, cleaning up the Solution. If the error in the VS2017, I will do my services by VS2015 and the App Xamarin for VS2017. It was worth all the same. I spent the whole Sunday, since 6:00 am and I will spend the morning, to resolve now the issue of notifications in Xamarin, but this is another post.

Browser other questions tagged

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