Brief background:
Using Database First/Code Only (though I don't think it should matter)
The basic setup is this:
public class MyContext : DbContext
{
public DbSet<MyClass_MyClasses> MyClass_MyClasses { get; set; }
public DbSet<MyClass> MyClasses { get; set; }
}
public class MyClass_MyClasses
{
[Key]
[Column(Order = 0)]
public Guid ParentId { get; set; }
[Key]
[Column(Order = 1)]
public Guid MyClassId { get; set; }
public int Sequence { get; set; }
public virtual MyClass MyClass { get; set; }
}
public class MyClass
{
[Key]
public Guid Id { get; set; }
public string Url { get; set; }
// ...
}
// interface part isn't important, I'm just using the wrapper to
// combine data from different sources to eventually be passed to a Json service
public class EntityWrapper : ISomeInterface
{
public MyClass_MyClasses Relation { get; set; }
public string Url { get { return MyClass_MyClasses.MyClass.Url; } }
// ... some other stuff
public EntityWrapper() { }
}
The problem seems to be that .Include(Func<,>) is not being honored when I do something like this:
using(MyContext context = new MyContext())
{
IEnumerable<EntityWrapper> wrappedResults =
from relation in context.MyClass_MyClasses.Include(mm => mm.MyClass)
orderby relation.Sequence ascending
select new EntityWrapper { Relation = relation };
foreach(EntityWrapper wrapper in wrappedResults)
{
// always thrown
if(wrapper.Relation.MyClass == null)
throw new WtfException("But I specified .Include?");
}
}
I've been working around it by instead of selecting to the EntityWrapper, selecting to the MyClass_MyClasses and then in my foreach just assigning a local variable = new EntityWrapper { ... } which works.
My question is: Am I doing something incorrectly? or is this a bug with EF?
Also: apologies in advance, this is a paraphrase of actual code so some of the things might not be exactly the same... If this isn't reproducible I'll try and copy more direct versions of my classes.
Thanks in advance.
public virtual MyClass MyClassproperty usingmodel builder?.Includebe enough? I didn't mention specifically in the question but I'm using Db First/Code only because I have some specifics I can't qualify using EF in the tables, though they should not interfere...Includeworks is incorrect. if you have configuredMyClassproperty correctly, it would have been lazy loaded..Includeshould have brought along theMyClassproperty with the initial query: msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx