I'm having trouble creating a solution for this business rule.
I currently have 1 Client, 1 Correspondent and 1 Shop.
Both use the same Address table.
Since the cliente
, correspondente
, and loja
tables can not have the auto-generated Id, I have decided to use GUID
to avoid duplicate error in the Endereco
table.
In this case the Endereco
table should look like this:
EnderecoId - ReferenceId - Logradouro
1 - GuidDoCorrespondente - Rua tal tal tal
2 - GuidDaLoja - Rua tal tal tal
3 - GuidCliente - Rua tal tal tal
But I'm having trouble mapping entities. Here's the template I'm trying to do:
EnderecoMap()
{
HasKey(x => x.EnderecoId);
Property(x => x.Logradouro)
.IsRequired()
.HasMaxLength(60);
Property(x => x.Numero)
.IsRequired();
HasRequired(x => x.Correspondente)
.WithMany(x => x.Enderecos)
.HasForeignKey(x => x.ReferenceId);
HasRequired(x => x.Cliente)
.WithMany(x => x.Enderecos)
.HasForeignKey(x => x.ReferenceId);
HasRequired(x => x.Loja)
.WithMany(x => x.Enderecos)
.HasForeignKey(x => x.ReferenceId);
}
I always get the following errors:
If I add a client and reference it like this:
public void AdicionandoLivro()
{
var cliente = new Cliente("Default",
"[email protected]");
cliente.AddEndereco(new Endereco(cliente.ClienteId, "Rua"));
_clienteRepository.Add(cliente);
_uow.Commit();
}
I get this error:
{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Endereco_dbo.Correspondente_ReferenceId\". The conflict occurred in database \"LivrariaEF\", table \"dbo.Correspondente\", column 'CorrespondenteId'.\r\nThe statement has been terminated."}'
When I add a Correspondente
with the similar code it generates the inverted error:
{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Endereco_dbo.Cliente_ReferenceId\". The conflict occurred in database \"LivrariaEF\", table \"dbo.Cliente\", column 'ClienteId'.\r\nThe statement has been terminated."}
If someone could help me, I would appreciate it!
I made the modifications as you suggested and my mapping looks like this:
ClienteMap()
HasMany(x => x.EnderecosCliente)
.WithOptional(x => x.Cliente)
.HasForeignKey(x => x.ClienteId);
CorrespondenteMap()
HasMany(x => x.EnderecosCorrespondente)
.WithOptional(x => x.Correspondente)
.HasForeignKey(x => x.CorrespondenteId);
- Do not need to put the dbset of Corresponding Addresses and Customer Addresses?
When I add in the Bank, it does not create anything in the Address table. Sorry for the Correspondent or Customer table. I am using this code to save in the database:
var correspondente = new Correspondente()
{
CorrespondenteId = Guid.NewGuid(),
Nome = "Walmart",
EnderecosCorrespondente = new List<EnderecoCorrespondente>()
{
new EnderecoCorrespondente() { Logradouro = "Rua 1", Numero = "1" },
new EnderecoCorrespondente() { Logradouro = "Rua 2", Numero = "2" }
}
};
_correspondenteRepository.Add(correspondente);
_uow.Commit();
Am I doing the correct map of the relationship?