c# - Code First Entity Framework Lazy Loading Not Working -
i'm having trouble getting lazy loading work.
if this:
static void main(string[] args) { using(var db = new blogcontext()) { //db.blogs.load(); //db.posts.load(); foreach (var v in db.blogs) { console.writeline("blog: "+ v.name+" post count:"+v.posts.count()); } } }
"post count" 0;
but if uncomment load() calls before foreach, post count correct. ideas what's wrong?
here's entity classes being used:
public class blog { public blog(){ posts = new list<post>(); } [key] public int blogid { get; set; } public string name { get; set; } public virtual icollection<post> posts { get; set; } } public class post { [key] public int postid { get; set; } public string title { get; set; } public string content { get; set; } public int blogid { get; set; } [foreignkey("blogid")] public virtual blog blog { get; set; } } public class blogcontext : dbcontext { public blogcontext() { configuration.lazyloadingenabled = true; configuration.proxycreationenabled = true; } public dbset<blog> blogs { get; set; } public dbset<post> posts { get; set; } }
turns out issue using local ms sql database. using sqlite doesn't have issues.