瓶颈使用实体框架继承 [英] bottleneck using entity framework inheritance
问题描述
所以我的实体
问题我想有一个页面与所有的故事标题和他们的类型
the problem i want to have a page with all the story titles and their types
有三个故事 LongStory
, CoOpStory
, GenericStory
通用故事与通用故事类型的 StoryType
相关联
the generic story is associated with StoryType
for the type of the generic story
我有两个问题
首先我想检查故事是否 LongStory
或 CoOpStory
我必须得到所有的实体,并检查实体类型,如果它是 LongStory
然后做某事,我不知道如何检索一些数据[只有标题],并检查类型
first when i want to check if the story is LongStory
or CoOpStory
i have to get all the entity and check the entity type if it is LongStory
then do something , i don't know how to retrieve some data [only the title] and check the type
我的第二个问题,就像我说我想要有一个页面,所有的故事标题和他们的类型,我不知道如何做到这一点没有做很多查询,我不能加入到 StoryType
表beca使用它必须是 GenericStory
与之相关联,所以我先做所有的故事,然后对每个 GenericStory
获取类型
my second problem like i said i want to have a page with all the story titles and their types i don't know how to do that without making a lot of queries , i can't make a join to the StoryType
table because it must be GenericStory
to be associated with that , so what i do first get all the stories then do an extra query for every GenericStory
to get the type
推荐答案
您正在查看 OfType
和 Concat
扩展方法。
You are looking of OfType
and Concat
extension methods.
只需要调用LongStory实例:
To get only LongStory instances you just need to call:
var longStories = context.Stories.OfType<LongStory>().ToList();
要获得更大的查询,它有点复杂。您可以尝试:
To get your bigger query it is little bit more complex. You can either try:
var allStories = context.Stories
.OfType<GenericStory>()
.Include("StoryType") // I'm not sure how this works with concat
.Concat(
context.Stories.OfType<LongStory>.Concat(
context.Stories.OfType<CoOpStory>()));
或者您可以做一个投影:
Or you can do a projection:
var allStories = context.Stories
.OfType<GenericStory>()
.Select(s => new { s.Title, s.StoryType.Name })
.Concat(
context.Stories
.OfType<LongStory>
.Select(s => new { s.Title, "LongStory" })
.Concat(
context.Stories
.OfType<CoOpStory>()
.Select(s => new { s.Title, Type })));
这篇关于瓶颈使用实体框架继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!