EF when add Entity, attach this also to mistake Entity but with same property

c# entity entity-framework entity-framework-6

Question

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

1
1
9/26/2018 3:44:50 PM

Popular Answer

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.

0
9/27/2018 10:36:17 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow