Ideally you do not use the Fluent API, rather an associative entity:
public class Cliente
{
/* Propriedades do Cliente */
public virtual EnderecoCliente EnderecoCliente { get; set; }
}
public class Endereco
{
/* Propriedades do Endereco */
public virtual EnderecoCliente EnderecoCliente { get; set; }
}
And then:
public class EnderecoCliente
{
[Key]
public int EnderecoClienteId { get; set; }
[Index("IUQ_EnderecoCliente_ClienteId_EnderecoId", IsUnique = true, Order = 1)]
public int ClienteId { get; set; }
[Index("IUQ_EnderecoCliente_ClienteId_EnderecoId", IsUnique = true, Order = 2)]
public int EnderecoId { get; set; }
public virtual Endereco Endereco { get; set; }
public virtual Cliente Cliente { get; set; }
}
Defining:
var endereco = new Endereco
{
/* Defina aqui outras propriedades */
EnderecoCliente = new EnderecoCliente {
Cliente = contexto.Clientes.FirstOrDefault(c => c.ClienteId == 5)
}
};
[Index]
, introduced in this form from the Entity Framework 6.1.0, ensures the uniqueness of the associative record. Additional validations may be required in the application to avoid strange key duplicity errors for the user.
as would be my model at the time of persistence of a new address , I would have a list of Addressecocliente, that’s it ?
– Leo
Not a list. A single record.
– Leonel Sanches da Silva
I misplaced the example. Now it’s right.
– Leonel Sanches da Silva
but the question is persistence, suppose that at the moment I am registering addresses this client has 3 or 4 different addresses. How do I persist if the model only receives a record ?
– Leo
So, see if it’s clearer now. I edited the answer.
– Leonel Sanches da Silva
just to understand I have a register that tells me the following, billing address, delivery address, home address, each in a different place , how to persist this ? Thank you @Ciganomorrisonmendez
– Leo
So you create using that syntax that I put up there. The block of creating the association with the client is the same for the 4.
– Leonel Sanches da Silva