abdulkhadar7
abdulkhadar7

Reputation: 125

Retrieve Values from database using Entityframework

enter image description hereenter image description hereI want to retrieve all rows which satisfying a condition. But when i tries only row count is correctly showing on debugging and the very first row value is repeatedly getting. following is my code

var data = (from jlist in entity.JobDetails
            where jlist.JobID == QJobID
            select jlist).ToList();

only first row value is showing in the var data. I have more than 1 items to be displayed

Upvotes: 1

Views: 246

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109109

Repetitive identical objects are nearly always a tell-tale of primary key inaccuracies: the PK that EF knows about does not uniquely identify the actual records in the database. This frequently happens when views are mapped to EF models, because a view is just a stored query that maybe doesn't even bother about unique identification.

In your case you changed the single primary key to a composite key, without EF knowing it, or you only told EF that JobID was the primary key.

When EF materializes an entity object it creates an EntityKey for it that has a reference to the entity. These EntityKey have to be unique, otherwise the change tracker crashes. So when there are two entities, identified by { 1, 1 } and { 1, 2 }, while EF only looks at the 1, EF will use an existing entity key for the second entity. The weird part, I think, is that EF still decides to materialize a second instance matching this entity key. If it wouldn't you'd have seen only one JobDetails record which might have better directed your suspicions to the right spot.

Upvotes: 2

Related Questions