Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different results in Entity Framework than LINQ to SQL

Tags:

sql

linq

entities

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!

like image 208
user390480 Avatar asked May 10 '11 13:05

user390480


1 Answers

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.

like image 110
Aducci Avatar answered Sep 22 '22 15:09

Aducci