2

I have an entity called Address. An Address contains a complex type, called House. A House contains a reference to its occupants. An Occupant is an entity.

public class Address {

    [key]
    public int Id { get; set; }

    public House House { get; set; }
}

The House:

[ComplexType] 
public class House
{

    [Required]
    public string HouseType { get; set; }


    public IList<Occupant> Occupants { get; set; }
}

The occupant

public class Occupant
{

    [key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public virtual Address Address { get; set; }

}

If I use lazyloading everything works fine and i can access all properties. However I need to use EagerLoading as the entities are needed long after the Context has been disposed.

I have tryed to include the properties using this code:

   // DbSet is of type DbSet<Address>
   List<Address> eagerLoadedEntity = DbSet.Where(a => a.Address.StartsWith("a"))
                .Include(a => a.House.Occupants).ToList();

I get the following error:

A specified Include path is not valid. The EntityType 'Address' does not declare a navigation property with the name 'House'.

CodeTower
  • 6,148
  • 5
  • 27
  • 50

2 Answers2

2

Maybe it is not possible at all? MSDN Complex Types

Complex types cannot participate in associations and cannot contain navigation properties.

You are treating "Occupants" as a navigation property on "House" on the "Include" statement and i suppose this might be the issue.

Anestis Kivranoglou
  • 6,398
  • 4
  • 38
  • 42
1

This is possible

check SO link to how to do nested eagerLoading

something like: This is just an example not tested... you will need to tweak.

   List<Address> eagerLoadedEntity = Context.Addresses
                         .Include("House")
                         .Include("House.Occupants")
                         .Where(a => a.Address.StartsWith("a"))
                         .ToList();

Update

Sorry i think you maybe correct about ComplexTypes.... but if they were all db entities then you should be able to do... something like... just an FYI

public class Address
{
    public int Id {get; set;}

    public int HouseId {get; set;}

    public string AddressLine1 { get; set;}

    public House House {get; set;}
}
public class House
{
    public int Id {get; set;}
    public string HouseType {get; set;}

    public virtual ICollection<Occupant> Occupants { get; set;}
}

public class Occupant
{
    public int Id {get; set;}

    public int HouseId {get; set;}
    public int PersonId {get; set;}

    public bool IsOwner {get; set;}
    public DateTime StartDate {get; set;}
    public DateTime EndDate {get; set;}

    public Person Person {get; set;}
    public House House {get; set;}
}

public class Person 
{
    public int Id {get; set;}
    public string FirstName {get; set;}
}

eager load

List<Address> eagerLoadedEntity = Context.Addresses
                         .Include("House")
                         .Include("House.Occupants")
                         .Where(a => a.Address.AddressLine1.StartsWith("a"))
                         .ToList();
Community
  • 1
  • 1
Seabizkit
  • 2,295
  • 2
  • 13
  • 29