I recently met a problem using linq on entities. On the db I have the following tables: User, Role, and UserToRole. Based on that model, the entity framework generated 2 entities: User and Role. An user can have multiple roles, and a role can gather multiple users.
The problem appears when trying to count the users with the role of student, for example (in other words how many students are in my db?).
The error I receive is:
LINQ to Entities does not recognize the method 'Boolean Contains[Role](System.Coll
Role], Role, System.Collections.Generic
[Role])' method, and this method cannot be translated into a store expression.
Role student = context.Role.FirstOrDefault( r => r.Name == "Student" );
int students = context.User.Where( u => u.Roles.Contains( student ) ).Count();