user3434888
user3434888

Reputation: 41

Update entites 1-1 Entity Framework Core

    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?

Upvotes: 0

Views: 42

Answers (1)

Dennis VW
Dennis VW

Reputation: 3217

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()
    }

Upvotes: 1

Related Questions