No insert in the table is inserting everything, even what I do not want

1

When I give savechanges() , it writes to all tables and I would like only one. How I do? The Acao and ProximaAcao tables also inserts log.

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 made it here, following the guidance of 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:

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

What can really 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 construct the XML do not know, just a WS REST that it mounts that XML according to the method response, right? I do not have that subject area, rs.

    
asked by anonymous 02.06.2014 / 22:28

1 answer

2

The problem is that you are opening two prominent context objects, 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;

The context understands that they are new objects. As the task is new, these two lines are not necessary. You can only assign the Ids of the existing objects:

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

As this insertion object is possibly incomplete, you will have to load it again. The return looks like this:

return db.T_TarefaParceiro.AsNoTracking().Include(t => t.Acao)
                                         .Include(t => t.ProximaAcao)
                                         .SingleOrDefault(t => t.Id == tarefa.Id);
    
02.06.2014 / 22:56