1

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

6
  • Can you show code, you are using for saving these two scenarios you described? Sep 26, 2018 at 12:19
  • No, i don't save this. Read only for now...
    – ricga
    Sep 26, 2018 at 14:54
  • Alright, then code you use for retrieving data from database? Sep 26, 2018 at 15:22
  • ok I modify your edit suggested
    – ricga
    Sep 26, 2018 at 15:45
  • What are datatypes of variables result and a? Sep 27, 2018 at 7:55

1 Answer 1

0

To be fair I am rather cunfused by your code and cant figure out what you were trying to do. I will try to provide correct approach for your problem based on how you described your entities and relationships.

Your entities:

public class Company
{
    public int id { get; set }
    public virtual ICollection<Sevice> Services { get; set; }
    public virtual ICollection<Container> Containers { get; set; }
}

public class Container
{
    public int id { get; set; }
    public int id_company { get; set; }
    public virtual Company company { get; set; }
    public virtual ICollection<Sevice> Services { get; set; }
}

public class Sevice
{
    public int id { get; set; }
    public int? id_company { get; set; }
    public int? id_container { get; set; }
    public virtual Company company { get; set; }
    public virtual Container container { get; set; }
}

You said that you are not adding any data to database yet, so lets focus on retrieving existing data.

 using (Context context = new Context())
 {
     var company = context.Companies.ToList();
 }

If you have disabled lazy loading then you need to include your child tables with include.

 using (Context context = new Context())
 {
     var company = context.Companies.Include(g => g.Services)
                                    .Include(g => g.Containers)
                                    .Include(g => g.Containers.Select(s => s.Services))
                                    .ToList();
 }

If you have correctly defined relationships your company object should have correctly loaded all objects.

If i am dead wrong about something let me know, so far good luck.

Final Edit

Lets have example data

   Company   { id: 1 }
   Container { id: 1, id_company: 1 }
   Service   { id: 1, id_company: 1, id_container: 1 }
   Service   { id: 2, id_company: 1, id_container: null }

Then with some get method like this:

using (Context context = new Context())
{
    var company = context.Companies.Where(x => x.id == someCompanyId)
                                   .Include(g => g.Services)
                                   .Include(g => g.Containers)
                                   .Include(g => g.Containers.Select(s => s.Services))
                                   .ToList();
}

You will end up with object that looks like this:

  Company
     services: (1, 2)
     containers: (1)
        services: (1)

Where Company is object, services and containers are collections of objects. Numbers in brackets are ids of enitity objects inside collections. You dont need to Add anything like in code you posted.

5
  • int id_container should be nullable -> int? id_container Sep 26, 2018 at 16:25
  • Yop, edited. Is this somehow helpful or your problem is related to something else? Sep 27, 2018 at 5:21
  • Yeah, if I take entity by include it's return correvt object, but if I add in a list like up I have double services(it's ok) and 1 container with 2 services(not ok, only one). Thanks for all
    – ricga
    Sep 27, 2018 at 7:20
  • i think i got you now. Your service that is in container must not have defined companyId. Either container_id or company_Id. If you have defined both then that service will appear in company service and company container. Sep 27, 2018 at 7:35
  • ok but my company.service is a list of all service. My container have the same or less, it's depend about id_container is valorized
    – ricga
    Sep 27, 2018 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.