Reputation: 490
My code:
DbContext.Musics()
.Include(m => m.Author)
.Select(m => new MyComplexType()
{
MyMusic = m,
blablabla = ...
})
The result MyComplexType.MyMusic.Author is null, I don't know how to solve it...
Upvotes: 1
Views: 1291
Reputation: 39916
This is a known issue in EF6 and prior, you can see a bug report in EF at https://github.com/aspnet/EntityFramework/issues/599
The behavior has been updated to allow Include(...)
into projections as of EF7-beta4, however for EF 6 or previous, this bug remains.
Upvotes: 5
Reputation: 101443
I don't think it's possible to do as you describe. When you select another type (not that type for which you did Include), Include no longer loads related entities, as you noticed yourself. Easy way to achieve what you want is:
DbContext.Musics()
.Select(m => new MyComplexType()
{
MyMusic = m,
Author = m.Author,
blablabla = ...
})
Update to asnwer complains from comments. Using author's code (and settings EnableLazyLoading to off on EF context), you will see that MyComplexType.MyMusic.Author is always null, even if Author is really in database. So Include does not work (does not do what you are expecting it to) in this particual case.
In code included in my answer, MyComplexType.Author will be correctly loaded (no need to use any Includes). So it's quite not the same as author's code.
Upvotes: 1