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

c# entity entity-framework entity-framework-6

Question

This kind of creature is mine.

n-Service Company and n-Service Company with n-Service Container

  • Service(id s, uuid c) -> Company(id s)
  • Service(id s, uuid c) -> Company(id s) -> Container(uuid c)

When I add a service to my firm, along with a container, I now have one service in its own container. When I add a second service to the same business without a container (or uuid), I end up with two services in one container.

Why is that?

Quick entity configuration schema.

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 database data retrieval

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 honest, your code confuses me a little and I'm not sure what you were attempting to do. Based on how you defined your entities and connections, I'll attempt to provide the best solution for your situation.

Its 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; }
}

Since you indicated you aren't yet adding any data to the database, let's concentrate on getting the data that is already there.

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

Your child tables must be included if lazy loading has been disabled.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();
 }

Your business object should have properly loaded all objects if relationships were appropriately established.

Let me know if I'm completely mistaken on anything; otherwise, good luck.

Final Edit

Let's use some sample 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 using a get method like as 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 eventually have an item that looks like this:

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

Services and containers are groupings of objects where Company is an object. Ids of entity objects included in collections are denoted by numbers in brackets. You don't need to make any modifications to the code you provided.

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