Question

I am using entity framework and raw sql queries. I have a view, but in some cases I would like to get only a few fields, because one field is a filestream and it has a big size.

However, when I use for example the following sql:

select Name, Surname from persons

I get an exception that says that the reader not have the field XXX. This is because my entity of the view, has all the fields of the view, but when exceute the query, the result does not return all the fields.

I would like to use raw sql and get a list of type persons, I mean that I would like to avoid the use of a var type.

I can't use lazy loading because I am using STEs, and the self tracking entities do not support lazy loading.

is it possible to do that? Select only a a few fields of the view, or I would create a new view only with the fields that I need? If this it correct, entity framework only can return all the fields always? this can be inefficient in some cases.

In summary, I would like to know if it is possible to select only some fields with STEs and raw sql.

Thanks.

Was it helpful?

Solution

Im not too sure about applying this to self tracking entities but the way you have always done this in the past is with an anonymous class (or a class you create) smaller than your entity:

context.People.Select(p=>new { Name = p.Name, Surname = p.Surname })

This will perform a SQL select similar to that listed in your question

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top