Ideally, you should not use the Fluent API , but 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, guarantees the uniqueness of the associative register. Additional validations may be required in the application to avoid extraneous errors of key duplication for the user.