Is it possible to use QueryView with EF Code-First?
-
13-10-2019 - |
سؤال
The title should be self explanatory. QueryView in EDMX allows predefined projections and conditional queries. QueryView creates additional readonly mapping.
Example:
Suppose that we have BlogPost
table which has Title
, ShortDescription
and Content
columns. If we want to use BlogPost
we will use directly this entity. If we want to show list of recent BlogPosts
showing only titles and descriptions we will not want to load contents as well. One way is to use query with projection directly. Other way is to map this projection in EDMX (by using QueryView = reusable projection is part of your mapping) to new entity called BlogPostInfo
. Then you can query your context for BlogPostInfo
entities when showing list of blog posts.
المحلول
I found that it is possilbe to use QueryView if DbContext
is generated from EDMX because in such case DbContext
only wrapps mapping in EDMX. But it is not what I was looking for. Code first itself probably doesn't offer any QueryView equivalent so the best way is probably define reusable projection queries as methods directly on derived DbContext
implementation.