I have very weird behavior with the first approach and EF code associations. I have two objects:
public class GlobalKpiSectionn { public GlobalKpiSection() { this.Regions = new HashSet<Region>(); } public virtual ICollection<Region> Regions { get; protected set; } } public class Region { public int RegionId { get; set; } public bool IsMain { get; set; } [Required] public virtual GlobalKpiSection KpiSection { get; set; } }
I need the required KiSection property attribute to get cascading deletes.
The problem is this: in this code:
var mainRegion = context.Regions.Single(x => x.RegionId == id); mainRegion.IsMain = true; context.SaveChanges();
I get an exception that the required field is not initialized. But he present just not loaded. I am not something to write explicit everywhere, it includes properties when I use this object. What can I do to overcome this? 
UPDATE
The reason I'm sure her lazy loading problem is this:
var primaryRegion = context.Regions .Include(x => x.KpiSection) .Single(x => x.RegionId == id);
Solves the problem, but its definitely a terrible solution.
source share