I've found that when using lazy child collections, the collection is loaded when an item is added directly to the context. This is a problem because the collection may be very large. I think EF wants to update the in memory collection with the new item, but it should realize that Í didn't request the collection.<br /><br />One workaround is to make the property non virtual. Another is to make the parent class internal (WTF!?)<br /><br />Here's code that reproduces the issue. Put a breakpoint in the last Add() call and confirm with SQL Server Profiler that all citizens (possible million, depends on the country) are loaded.<br /><br />public class Country // make internal to avoid spurious select<br />{<br /> public virtual int CountryID { get; set; }<br /><br /> public virtual ICollection<Citizen> Countries { get; set; } // make non virtual to avoid select<br />}<br />public class Citizen<br />{<br /> public virtual int CitizenID { get; set; }<br /><br /> public virtual int CountryID { get; set; }<br />}<br />class Context : DbContext<br />{<br /> public DbSet<Country> Countries { get; set; }<br /> public DbSet<Citizen> Citizens { get; set; }<br />}<br />class Program<br />{<br /> static void Main()<br /> {<br /> using (var db = new Context())<br /> {<br /> db.Countries.Add(new Country());<br /> db.SaveChanges();<br /> }<br /> using (var db = new Context())<br /> {<br /> var c = db.Countries.FirstOrDefault();<br /> db.Citizens.Add(new Citizen { CountryID = c.CountryID, }); // loads all citizens of the country<br /> }<br /> }<br />}<br />
Comments: We have not been able to reproduce this issue with the code you provided. Assuming the database has multiple citizens for the given country, if I then execute the code you provided to add a new citizen the navigation property only contains one item. (It contains one item because EF does fixup on the navigation property.) It does not contain all the items that are in the database but not loaded. There are cases where the entire navigation property collection will be loaded, including any time the navigation property is accessed explicitly for the first time, but it is hard to know how to advise the best way to approach these without a working repro of the actual problem you are having. Thanks, Arthur
Comments: We have not been able to reproduce this issue with the code you provided. Assuming the database has multiple citizens for the given country, if I then execute the code you provided to add a new citizen the navigation property only contains one item. (It contains one item because EF does fixup on the navigation property.) It does not contain all the items that are in the database but not loaded. There are cases where the entire navigation property collection will be loaded, including any time the navigation property is accessed explicitly for the first time, but it is hard to know how to advise the best way to approach these without a working repro of the actual problem you are having. Thanks, Arthur