I have a view defined in the database (archiveContentPreviews), It joins together several tables and in Linq it has one entity key (ArchiveID), I want to query this view with this simple query:
var x = from fields in entities2.archiveContentPreviews
where fields.ArchiveID == archiveID
select fields;
return x.ToList<archiveContentPreview>();
The problem that it returns exact number of results but multiple copy of the first result, and when I execute that query in SQL management studio it returns correct results, any help?!
This typically happens when the column (or columns) designated as primary key have no unique values in the view. In your case, ArchiveID
is probably repeated in a large number of view rows (which is also indicated by your where
clause). You will have to find (or add to the view) a combination of columns that uniquely identify a view row and mark those as primary key in the EF model.
Note that the data returned by the generated SQL query may contain rows with different values (but the same ArchiveID
), but EF just materializes entity objects for each ArchiveID
with the first result it can find for that id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With