In the table Insert is inserting everything, until what I do not want

Asked

Viewed 129 times

1

When I give a savechanges(), records to all tables and would like just one. How do I do? Tables Acao and ProximaAcao also inserts record.

public T_TarefaParceiro geraIdParceiro()
{
    WEBEntities db = new WEBEntities();
    T_ParceiroOs osParceiro = new T_ParceiroOs();
    T_TarefaParceiro tarefa = new T_TarefaParceiro();
    T_ParceiroTarefa _tarefa = new T_ParceiroTarefa();
    T_Acao t = new T_Acao();
    T_ProximaAcao tp = new T_ProximaAcao();
    int _idAcao = 0;
    int _idProxima = 0;

    try
    {
        var tipoEstabelecimento = new List<int>{2,3,4,5,6};

        if (geraIdNovoPdv() == true)
        {
            var novo = db.T_PDV.Join(
                db.T_CRM_StatusPDV, t1 => t1.CNPJ, t2 => t2.DE_Cnpj,
                (t1, t2) => new { t1, t2 })
                .Where(c => c.t2.DT_TransacaoV == null && tipoEstabelecimento.Contains(c.t1.IDTipoEstabelecimento))
                .Select(i => new { i.t1.CNPJ });

            var acao = db.T_Acao.Join(db.T_ProximaAcao,
                t1 => t1.IDAcao, t2 => t2.IDAcao,
                (t1, t2) => new { t1, t2 })
            .Where(a => a.t1.IDAcao == a.t2.IDAcao && a.t1.IDAcao == 7)
            .Select(i => new { i.t1.Acao, i.t2.ProximaAcao, i.t1.IDAcao, i.t2.IDProximaAcao});

            foreach (var lista in novo)
            {
                osParceiro.PdvNovo = 1;
                osParceiro.Cnpj = lista.CNPJ;
            }

            foreach (var lista in acao)
            {
                osParceiro.AcaoParceiro = lista.Acao;
                osParceiro.ProximaAcao = lista.ProximaAcao;
                _idAcao = lista.IDAcao;
                _idProxima = lista.IDProximaAcao;
                t.Acao = lista.Acao;
                tp.ProximaAcao = lista.ProximaAcao;
            }

            tarefa.CNPJ = osParceiro.Cnpj;
            tarefa.IDAcao = _idAcao;
            tarefa.IDProximaAcao = _idProxima;
            tarefa.T_Acao  = t;
            tarefa.T_ProximaAcao = tp;

            db.T_TarefaParceiro.Add(tarefa);
            db.SaveChanges();
        }
        else if (geraIdPdvV99() == true)
        { 
        }

    }
    catch (Exception ex)
    {
        string erro = ex.Message;
    }

    return tarefa;
}

I did it here, following the guidance of the Gypsy and now the browser does not show. I think it has to do with the return XML.

t.IDAcao = lista.IDAcao;
t.Acao = lista.Acao;
tp.IDProximaAcao = lista.IDProximaAcao;
tp.ProximaAcao = lista.ProximaAcao;

And I called it that:

tarefa.T_Acao = t;
tarefa.T_Acao = t;

What really might be happening?

It is a web service REST. The interface of this method is this:

[OperationContract]
        [WebInvoke(
            Method = "GET", // Tipo de request
            BodyStyle = WebMessageBodyStyle.Bare, // Identação do retorno
            UriTemplate = "abreos" // Url do serviço, onde cada {} = parametro
            )]//Filter para tratar REST
        T_TarefaParceiro geraIdParceiro();

How do I build XML I don’t know, just a WS REST that assembles this XML according to the method’s response, right? I do not have this domain of the subject, rs.

  • I laughed with that title. I already answer you.

  • Sometimes even the title gets hard to choose

1 answer

2

The problem is that you are opening two objects in theory highlighted from the context, here:

foreach (var lista in acao)
{
    osParceiro.AcaoParceiro = lista.Acao;
    osParceiro.ProximaAcao = lista.ProximaAcao;
    _idAcao = lista.IDAcao;
    _idProxima = lista.IDProximaAcao;
    t.Acao = lista.Acao;
    tp.ProximaAcao = lista.ProximaAcao;
}

These variables _idAcao and _idProxima are not necessary. When you do this assignment:

tarefa.IDAcao = _idAcao;
tarefa.IDProximaAcao = _idProxima;

Context understands that they are new objects. Since the task is new, these two lines are not necessary. You can assign Ids only to existing objects:

tarefa.T_Acao  = t;
tarefa.T_ProximaAcao = tp;

Since possibly this object being inserted is quite incomplete, you will have to load it again. The return is like this:

return db.T_TarefaParceiro.AsNoTracking().Include(t => t.Acao)
                                         .Include(t => t.ProximaAcao)
                                         .SingleOrDefault(t => t.Id == tarefa.Id);
  • this way the browser does not display the return xml.

  • Ask your question how you assemble this XML to improve the answer.

  • Forgive my ignorance, but is this the return of the method? And the other fields? I confess that I had a herd in this.

  • It is the return. What is done there is to reload the record out of context and return it. Methods Include() force explicit loading of data from Acao and ProximaAcao.

Browser other questions tagged

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