Reputation: 5008
I have 4 models
Factory can have many cars, a Car can have TypeA and TypeB. Both TypeA and TypeB can exist on the car i.e im using eager-loading So my models would look like
public class Factory
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int FactoryID {get;set;}
public ICollection<Car> Cars {get;set;}
}
public class Car
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CarID {get;set;}
public int FactoryID {get;set;}
public Factory Factory {get;set;}
//The Types
public TypeA TypeA {get;set;}
public TypeB TypeB {get;set;}
}
public class TypeA
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TypeAID {get;set;}
[Key, ForeignKey("Car")]
public int CarID {get;set;}
public Car Car {get;set;}
}
public class TypeB
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TypeBID {get;set;}
[Key, ForeignKey("Car")]
public int CarID {get;set;}
public Car Car {get;set;}
}
Here is my problem. How do I load both TypeA and TypeB into my Factory Object (using Eager-Loading)
Factory factory = db.Factory.Where(f => f.FactoryID == 1).Include(f => f.Cars.Select(c => c.TypeA) ... .FirstOrDefault();
Because I cannot say
Include(f=>f.Cars.Select(...).Select(...))
Upvotes: 1
Views: 37
Reputation: 2255
Just chain the include
statement:
Factory factory = db.Factory.Where(f => f.FactoryID == 1)
.Include(f => f.Cars.Select(c => c.TypeA))
.Include(f => f.Cars.Select(c => c.TypeB))
....;
By the way:
It may would make sense to introduce a base class for TypeA
and TypeB
- or even just use one class, since I don't see a difference between the two of them.
Upvotes: 1
Reputation: 62093
You can have multiple include statements. There is no need to only have one.
Upvotes: 1