19

This is my first time using the Entity Framework and I'm getting some confusing results. I know a particular table contains 3 distinct rows when I run this SQL query:

SELECT * FROM mytable WHERE service_month = 201012

When I run this query against the framework however, I get 3 rows, but they are all copies of the first row (VB syntax).

Dim temp = _context.mytable.Where(Function(x) x.service_month = 201012)

Did I set up something incorrectly? This is how I'd do it with LINQ to SQL so I feel like I'm missing something.

1
  • what are you then doing with temp to check the values? Commented Aug 31, 2011 at 12:56

2 Answers 2

41

Fix your primary key definition in your EDMX. (If your table has no PK, add one.) When all rows return the same "key", the EF returns the same object instance.

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

4 Comments

Bingo. I'm using reporting tables set up by someone else and they didn't have a primary key. Thanks.
Even if the developers call this a feature, I'm calling it a bug. Thanks a lot for the solution.
Thanks @Craig. Just now found out that the table i'm working on, has no primary key.
Wow. Was seleting from a view that had no key. I had to set the Date field (only unique field in view) to the EntityKey in the edmx and all is fine.
0

I experienced this in Code First 5.0.13 when I had used a [Key] attribute in the business object that came from the wrong library. It should be

System.ComponentModel.DataAnnotations.KeyAttribute

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.