I have this type of entity
Company with 'n' Service and Company with 'n' Container with 'n' Service
- Company(id_s) -> Service(id_s, uuid_c)
- Company(id_s) -> Container(uuid_c) -> Service(id_s, uuid_c)
When I add a service, with container, to company... I have 1 service and 1 container with 1 service. When I add another service, without container(and uuid), to the same company... I Have 2 services and 1 container with 2 Services! :(
Why this?
Fast schema of my entity configuration.
Container:
HasRequired(e => e.company)
.WithMany(e => e.container)
.HasForeignKey(e => e.id_company)
.WillCascadeOnDelete(false);
Service:
HasOptional(e => e.container)
.WithMany(e => e.services)
.HasForeignKey(e => e.uuid_container)
.WillCascadeOnDelete(false);
Code for retrieving data from database
if (!result.Any(x => x.id == cfi.id_company))
{
result.Add(cfi.Company);
}
else
{
result.Where(x => x.id == cfi.id_company).FirstOrDefault().result
.Where(x => x.id == cfi.id_company).FirstOrDefault();
a.services.Add(cfi.Company.services.FirstOrDefault());
}
cfi is a list of services, with include company that have container and service