Conversion error when trying to insert into the bank with Entity and c#

Asked

Viewed 26 times

1

This is the method to insert:

public virtual void Inserir(T item) 
        {
            contexto.Set<T>().Add(item);
            contexto.SaveChanges();
        }

I created a method by passing the parameters to my object and then calling the insert method, so(I tried to avoid "linguing", but could not):

private void InserirBalanca(int ticket, DateTime dtlancamento, string fornecedor, string motorista, string placa, decimal peso,
                                    decimal umidade, decimal impureza, decimal pesoliq, decimal desconto, decimal valorsec, decimal saca, 
                                    decimal tabela, string pesagem)
        {
            SiloContext contexto = new SiloContext();
            IRepositorio<Balanca> insere = new Repositorio<Balanca>(contexto);

            var balanca = contexto.Balancas;

            foreach(var blc in balanca)
            {
                blc.Ticket = ticket;
                blc.DtLancamento = dtlancamento;
                blc.Fornecedor = fornecedor;
                blc.Motorista = motorista;
                blc.Placa = placa;
                blc.Peso = peso;
                blc.Umidade = umidade;
                blc.Impureza = impureza;
                blc.PesoLiq = pesoliq;
                blc.Desconto = desconto;
                blc.ValorSec = valorsec;
                blc.Saca = saca;
                blc.Tabela = tabela;
                blc.Pesagem = pesagem;
                balanca.Add(blc);
            }

            insere.Inserir(balanca);
        }

Along those lines insere.Inserir(balanca);, is giving cast error, see the image:

inserir a descrição da imagem aqui

I checked this error on the net, but it did not come out, I believe it is because of the name Balanca.

  • Data is received to enter a single correct balance? if yes, what is the reason to perform a for each?

1 answer

2


dude, your balanca comes from contexto.Balancas; therefore, it is a DbSet. The method inserir expects to receive an object Balanca

also did not understand the pq of foreach, but according to your code what you are trying to do should stay +- so:

        foreach(var blc in balanca)
        {
            blc.Ticket = ticket;
            blc.DtLancamento = dtlancamento;
            blc.Fornecedor = fornecedor;
            blc.Motorista = motorista;
            blc.Placa = placa;
            blc.Peso = peso;
            blc.Umidade = umidade;
            blc.Impureza = impureza;
            blc.PesoLiq = pesoliq;
            blc.Desconto = desconto;
            blc.ValorSec = valorsec;
            blc.Saca = saca;
            blc.Tabela = tabela;
            blc.Pesagem = pesagem;
            insere.Inserir(blc);
        }

Edit:

As I and Alexandre commented, we do not understand why the foreach, IE, why you go through all the records stored in the bank, and exit inserting all again with the new values passed. You should just create a new object Balanca and insert. It would look like this:

    private void InserirBalanca(int ticket, DateTime dtlancamento, string fornecedor, string motorista, string placa, decimal peso,
                                decimal umidade, decimal impureza, decimal pesoliq, decimal desconto, decimal valorsec, decimal saca, 
                                decimal tabela, string pesagem)
    {
        SiloContext contexto = new SiloContext();
        IRepositorio<Balanca> insere = new Repositorio<Balanca>(contexto);

        var blc = new Balanca();
        blc.Ticket = ticket;
        blc.DtLancamento = dtlancamento;
        blc.Fornecedor = fornecedor;
        blc.Motorista = motorista;
        blc.Placa = placa;
        blc.Peso = peso;
        blc.Umidade = umidade;
        blc.Impureza = impureza;
        blc.PesoLiq = pesoliq;
        blc.Desconto = desconto;
        blc.ValorSec = valorsec;
        blc.Saca = saca;
        blc.Tabela = tabela;
        blc.Pesagem = pesagem;

        insere.Inserir(blc);
    }

I hope I’ve helped.

  • I did the foreach, because I do not know how to do otherwise, ie, load my object with data and then persist. As I said, I couldn’t avoid the sausage and I’m even looking for another way to do it, which is not that way, but for now I’m testing it like this. I had already resolved, but I marked your reply.

  • @pnet edited the answer to explain the foreach question, see if it helps you.

  • Okay, it did get better.

Browser other questions tagged

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