5

I was first using LINQ to SQL in my project and used the following statement:

var ProjectRouteEmails = EmailManagerDAL.Context.ProjectRouteEmails
            .Where(p => p.ProjectID == ProjectID);

That correctly returned the three distinct emails from the view ProjectRouteEmails. The IDs returned from the Emails table were 117, 591, and 610.

I changed to LINQ to Entities and use the same view and same LINQ statement, but even though I am getting back three records, it is the first record, ID 117, that is getting returned three times.

I tried writing the LINQ statment like this:

var ProjectRouteEmails = from p in EmailManagerDAL.Context.ProjectRouteEmails
                                 where p.ProjectID == ProjectID
                                 select p;

but it made no difference; the same record returned three times.

I went into SQL Server Management Studio and ran the query:

select * from ProjectRouteEmails (nolock) 
where ProjectID = 12

and the correct three, unique records returned.

What is going on here?

Thanks!

user390480
  • 1,655
  • 4
  • 28
  • 61
  • I would double check how you're iterating through this to output... sounds fishy – hunter May 10 '11 at 13:19
  • I am just binding it to a Grid. Also I have a breakpoint set and examining it in the watch window shows the same results. – user390480 May 10 '11 at 13:24

1 Answers1

5

Make sure the entity key is set correctly for ProjectRouteEmails in the Entity Data Model. Sometimes the entity keys are messed up when you import the view into the model.

Aducci
  • 26,101
  • 8
  • 63
  • 67
  • Interesting...can you provide specifics or more detail? Is it in the entity object? Or...some relational metadata? Some may use the tools to create the entity data model without digging into the nuts & bolts. – IAbstract May 10 '11 at 14:07
  • THAT WAS IT!!!! Thank you Aducci! It seemed to have arbitrarily selected a text field as the entity key. I changed it to the correct field and it works. Thank you! – user390480 May 10 '11 at 14:10
  • 2
    Just for future reference for others. Open the edmx then your view right click on the field and add entity key. – causita Jul 10 '14 at 16:19