entity framework - LazyLoadingEnabled setting doesn't seem to work in EF 5 -
i'm using ef model first poco entities , custom dbcontexts. problem setting lazyloadingenabled=false not affect anything, navigation properties still loaded. below example simplified.
the entity program. program can part of other programs:
namespace domain.entities { using system; using system.collections.generic; public partial class program { public program() { this.programs = new hashset<program>(); } public int id { get; set; } public string title { get; set; } public string description { get; set; } public system.datetime startdate { get; set; } public system.datetime enddate { get; set; } public nullable<int> programid { get; set; } public virtual icollection<program> programs { get; set; } public virtual program ownerprogram { get; set; } } } the dbcontext:
namespace infrastructure.model { public class programcontext : dbcontext { public programcontext() : base("name=mycontainer") { configuration.lazyloadingenabled = false; } public dbset<program> programs { get; set; } } } here how use it:
private programcontext _dbcontext = new programcontext(); // api/program public ienumerable<program> getprograms() { list<program> list = _dbcontext.programs.tolist(); return list; } with above sample, ef still loads programs , ownerprogram properties of program class. have tried removing virtual keywords, disabling proxy creation , verified lazyloadingenabled=false on model itself.
am missing something?
the effect seeing called relationship fixup.
actually navigation properties not loaded explicitly. query _dbcontext.programs.tolist() loads whole programs table database. simple sql query (like select * programstable) without where clause , without join related rows.
also no lazy loading happens here (it can't if disable , if disable dynamic proxies) when access program.programs , program.ownerprogram navigation properties.
the navigation properties populated when result query materialized because query (that loads all programs) have loaded programs navigation properties can refer to. ef detects related entities in memory , put them navigation properties automatically.
you can verify if don't load all programs only, example, single one:
program program = _dbcontext.programs.firstordefault(); now, program.programs , program.ownerprogram null - unless loaded program part of own program.ownerprogram collection or own ownerprogram.
Comments
Post a Comment