Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Query returns multiple copies of first result

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?!

like image 643
Lisa Avatar asked Feb 11 '13 11:02

Lisa


1 Answers

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.

like image 150
Gert Arnold Avatar answered Sep 28 '22 02:09

Gert Arnold