none
EF + ADO.Services + Update RRS feed

  • Pergunta

  • Olá pessoal... alguém sabe, já passou por isso.. Ajuda!!!

    Como faço uma atualização em FK de uma tabela????

    Ex:
    Tenho tabelas de InvoiceNoteCurrent e InvoiceNoteCurrentStatus.. 
    na tabela InvoiceNoteCurrent tenho vinculo com a tabela InvoiceNoteCurrentStatus.. pelo campo InvoiceNoteCurrentID.
    ..

    Se eu tenho seguintes resultados. nas tabelas
    Tabela InvoiceNoteCurrent
    ==========================================
    Name     | InvoiceNoteCurrentID  ModifiedDate             |
    ----------------------------------------------------------------------------
    produto1 | 1                             | 2009-11-12 00:01:02.627
    produto2 | 1                             | 2009-11-12 00:01:04.444
    produto3 | 3                             | 2009-11-12 00:01:07.607
    produto4 | 2                             | 2009-11-12 00:01:02.627
    ==========================================

    Tabela InvoiceNoteCurrentStatus
    ======================
    InvoiceNoteCurrentID | Name
    ---------------------------------------
                   1              | Aberto
                   2              | Conferido
                   3              | Divergencia
    ======================


    Como atualizar a tabela InvoiceNoteCurrent.. trocando o InvoiceNoteCurrentID do produto3 de 3 para 1??????

    Consigo atualizar qualquer campo na tabela com o codigo abaixo.. no qual atualizo o campo data, Mais não consigo alterar O fk...InvoiceNoteCurrentID

    Tentei da forma abaixo Ele passa sem erros. mais não atualiza o FK, somente o campo ModifiedDate 

    [ Member Variables ]
    private ConferenceEntities contextConferenceOverwrite;
    private InvoiceNoteCurrents currentInvoiceNoteCurrent;
    private InvoiceNoteStatuss currentInvoiceNoteStatus;
    private DataServiceQuery<InvoiceNoteStatuss> queryInvoiceNoteStatus;

    [ LOAD ]
    this.contextConferenceOverwrite = new ConferenceEntities(new Uri("http://localhost:1466/services/ConferenceService.svc", UriKind.RelativeOrAbsolute));
    this.contextConferenceOverwrite.MergeOption = MergeOption.OverwriteChanges;

    [ UPDATE ]
    string filterExpressionInvoiceNoteStatus = "Name eq 'Conferido'";
    this.queryInvoiceNoteStatus = contextConference.InvoiceNoteStatus.AddQueryOption("$filter",filterExpressionInvoiceNoteStatus);
    this.queryInvoiceNoteStatus.BeginExecute(OnInvoiceNoteStatusQueryComplete, this.queryInvoiceNoteStatus);

    private void OnInvoiceNoteStatusQueryComplete(IAsyncResult result)
    {
         DataServiceQuery<InvoiceNoteStatuss> query = result.AsyncState as DataServiceQuery<InvoiceNoteStatuss>;
         InvoiceNoteStatuss returnedInvoiceNoteStatus = query.EndExecute(result).FirstOrDefault();
         if (returnedInvoiceNoteStatus != null)
         {

             this.currentInvoiceNoteStatus = returnedInvoiceNoteStatus;
            
             contextConferenceOverwrite.AttachTo("InvoiceNoteCurrent", this.currentInvoiceNoteCurrent);
             this.currentInvoiceNoteCurrent.ModifiedDate = DateTime.Now;

             contextConferenceOverwrite.AttachTo("InvoiceNoteStatus", this.currentInvoiceNoteStatus);
             this.currentInvoiceNoteCurrent.InvoiceNoteStatus = this.currentInvoiceNoteStatus;

             contextConferenceOverwrite.UpdateObject(this.currentInvoiceNoteCurrent);
             contextConferenceOverwrite.BeginSaveChanges(SaveChangesOptions.ReplaceOnUpdate, OnChangesSaved, contextConferenceOverwrite); 
       
         }
    }

    private void OnChangesSaved(IAsyncResult result)
    {
        Dispatcher.BeginInvoke(() =>
        {
             contextConferenceOverwrite = result.AsyncState as ConferenceEntities;
             DataServiceResponse response = contextConferenceOverwrite.EndSaveChanges(result);
             if (response == null)
             {
                  throw new DataServiceQueryException("Erro xxxxxx!!!");
             }
         });
    }

    sexta-feira, 13 de novembro de 2009 14:36

Respostas