Mustang31
Mustang31

Reputation: 282

Different results with selector LINQ on a database view

When I use against a dbase(oracle) View

from f in MYVIEW
where f.Order=="HERE"
select f

I get identical results(rows).

With...

from f in MYVIEW
where f.Order=="HERE"
select f.ColA

I get the correct results returned for the ColA

I know this must be a newbie question. Still learning...thanks in advance

Upvotes: 1

Views: 240

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

The reason is that EF by default needs to identify uniquely every record. Because of that every entity must have unique key. Views don't have a key so EF infers the key by using all non-nullable columns which don't contain binary data. Now EF expects that these columns will make the entity uniquely identifiable. If they don't you will end with the problem you see in your first example. When EF reads entities from result set it check the key and if the entity with the same key was already created it uses that instance instead of creating a new one -> all records from the result set with the same values in key columns will be represented by the same entity instance. I described today how to avoid this problem with views.

Your second example uses projection. In such case no entity instances are constructed and EF simply returns values.

Upvotes: 3

Related Questions