2
I’m having trouble creating a solution to this business rule.
I currently have 1 Customer, 1 Correspondent and 1 Store.
Both use the same address table.
Like the tables cliente
, correspondente
and loja
cannot have the Id self manageable, I decided to use GUID
to avoid duplication error in the table Endereco
.
In this case the table Endereco
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 the entities. Follow the model I’m trying to make:
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 customer and reference 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 that mistake:
{"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 similar code generates me reversed 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 anyone could help me, I’d really appreciate it!
I made the modifications as you suggested and my mapping was 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);
- No need to put the corresponding Address dbset and Addressclient?
When I add it to the Database, it creates nothing in the Address table. Only in the Corresponding table or Client table. I’m using this code to save in the bank:
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 making correct relationship map?
This error occurs when you do the mapping and create the migration or when you will insert ? Your question left me confused about this. If you are entering, you are entering the customer before the address?
– G. M4rc14L
I updated the question with more explanations. VLW!
– allan.egidio
"You don’t need to put the dbset of Addresscorresponding and Addressclient?" you do. By the way, you also need one more:
public DbSet<Endereco> Enderecos { get; set; }
– Leonel Sanches da Silva
Really just was missing the dbSet of Addresscorresponding and Addresscustomer. Thanks again for the Gypsy force. Hugs!
– allan.egidio