I have 1:n relationship between to tables in a database: Employee (1) : Role (n)
Role has foreignKey named idEmployee
I want to create a linq statement which will get every role for a given customer. I want to make something like this:
var myQuery = from r in Role
where r.idEmployee == someId
select r;
But, r doesn’t have an idEmployee property! How can I get the value of the foreign key?
If you are using
EF4.0at least, this will give you what you need: Foreign keys in Entity FrameworkIf you’re using
EF1.0, your problem is more serious since it does not show foreign keys in model. You need to iterate throughEntityKey.EntityKeyValuescollection in search of valid value. But I think this would only get you value of foreign key and would not work in query (since EF would not know how to translate it to SQL query).But since you have foreign keys, why don’t you simply use
NavigationPropertyto navigate toEmployeeentity and check value there?