1

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

10
  • @GiladGreen how to add picture Commented Oct 21, 2016 at 14:01
  • 1
    How are you debugging? Linq queries are tricky to debug since they often use deferred execution and can be wrappers over larger collections. Commented Oct 21, 2016 at 14:06
  • i have added pictures of both output and dubugging. Commented Oct 21, 2016 at 14:09
  • This indicates that the primary key of JobDetails isn't defined correctly. Commented Oct 21, 2016 at 14:10
  • 1
    @GertArnold After updating entity model code is working fine Commented Oct 21, 2016 at 14:26

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.