kitndirangu
kitndirangu

Reputation: 145

EF 5. Code First DbContext Child objects not mapping on retrieving from DB

I have this class;

[Table("tblRegions")]
public class Region : MasterEntity
{
      public string Code { get; set; }
      public string Description { get; set; }
      public Region ParentRegion { get; set; }
      public Country Country { get; set; }
      public RegionType RegionType { get; set; }
}

it turns out Region, Country RegionType fields are created as foreign key fields in DB and ID values saved correctly.

Problem is on retrieving a Region, ParentRegion, Country and RegionType are null yet in the db I see their Id values.

Upvotes: 0

Views: 248

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

You forgot to mark them as virtual.

 public virtual Region ParentRegion { get; set; }
 ...

This is how you let EF to create overridden properties within autogenerated proxy classes that lazy load your parent entities.

Upvotes: 1

Related Questions