0
    public async Task SaveAsync(Cliente cliente)
    {
        if (cliente.Id == Guid.Empty)
        {
            _cadastroContext.Clientes.Add(cliente);
        }
        else
        {
            _cadastroContext.Clientes.Update(cliente);
        }

        await _cadastroContext.SaveChangesAsync();
    }

The Cliente class has a 1-1 relationship with the Conta class, when I save a new record, both classes are saved simultaneously, but when I go to update it is not updated to the Conta class.

After the Update command is executed, the properties of the Conta class are the same as those in the database and not the same as those sent.

How can I make two composite classes update?

1
  • Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your full source code you have as a minimal reproducible example, which can be compiled and tested by others. Do not include any screenshot of your source code. Commented Oct 19, 2019 at 9:06

1 Answer 1

1

You need to include the conta objects before updating the clientes object.

public async Task SaveAsync(Cliente cliente) 
{
    ... code ommited for brevity
    else 
    {
        var savedCliente = _cadastroContext.Include(c => c.Conta).FirstOrDefault(c = c.Id == cliente.Id);
        savedCliente = cliente;

        _cadastroContext.Update(savedCliente);
        await _cadastroContext.SaveChangesAsync()
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, This code worked for me, however I needed to use AsNoTracking().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.