In EF eager loading related entities is easy.
But I'm having difficulties including inherited entities when loading data using table-per-type model.
This is my model:
Entities:
ArticleBase
(base article entity)ArticleSpecial
(inherited fromArticleBase
)
UserBase
(base user entity)UserSpecial
(inherited fromUserBase
)
Image
Relations are as shown on the image (omitting many columns):
In reality my users are always of type UserSpecial
, since UserBase
is used in another application, thus we can share credentials. That's the only reason I have two separate tables. UserBase
table can't be changed in any way shape or form, because the other app would break.
Question
How am I suppose to load ArticleSpecial
with both CreatedBy
and EditedBy
set, so that both are of type UserSpecial
(that defines Image
relation)?
I've tried (unsuccessfully though) these options:
1. Using lambda expressions:
context.ArticleBases
.OfType<ArticleSpecial>()
.Include("UserCreated.Image")
.Include("UserEdited.Image");
In this case the problem is that both CreatedBy
and EditedBy
are related to UserBase
, that doesn't define Image
navigation. So I should somehow cast these two to UserSpecial
type like:
context.ArticleBases
.OfType<ArticleSpecial>()
.Include("UserCreated<UserSpecial>.Image")
.Include("UserEdited<UserSpecial>.Image");
But of course using generics in Include("UserCreated<UserSpecial>.Image")
don't work.
2. I have tried using LINQ query
var results = from articleSpecial in ctx.ArticleBase.OfType<ArticleSpecial>()
join created in ctx.UserBase.OfType<UserSpecial>().Include("Image")
on articleSpecial.UserCreated.Id equals created.Id
join edited in ctx.UserBase.OfType<UserSpecial>().Include("Image")
on articleSpecial.UserEdited.Id equals edited.Id
select articleSpecial;
In this case I'm only getting ArticleSpecial
object instances without related properties being set. I know I should select those somehow, but I don't know how?
Select part in my LINQ could be changed to something like
select new { articleSpecial, articleSpecial.UserCreated, articleSpecial.UserEdited };
but images are still not loaded into my context. My joins in this case are barely used to filter out articleSpecial results, but they don't load entities into context (I suppose).
Can anybody provide any help regarding this problem? I think it's not so uncommon.