gfrizzle
gfrizzle

Reputation: 12609

Entity framework query returns the same row multiple times

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.

Upvotes: 19

Views: 9911

Answers (2)

Kirsten
Kirsten

Reputation: 18076

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

Upvotes: 0

Craig Stuntz
Craig Stuntz

Reputation: 126547

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.

Upvotes: 40

Related Questions