The foreign key is obtained through the entity framework.

c# entity-framework linq

Question

In a database, I have a 1:n connection between two tables: Personnel (1): Role (n) Role's idEmployee foreignKey

I want to write a Linq query that would get each position for a certain client. I want to create something similar to this:

var myQuery = from r in Role 
              where r.idEmployee == someId
              select r;

But idEmployee is not a property of r! How can I determine the foreign key's value?

1
1
12/27/2011 7:44:38 PM

Accepted Answer

If you useEF4.0 This will at least provide you with what you require: Entity Framework foreign keys

If you useEF1.0 Your issue is more significant since the model doesn't display any foreign keys. You must repeat the process.EntityKey.EntityKeyValues collection looking for a good bargain. However, I believe that this would simply return the value of a foreign key and not function in a query (since EF would not know how to translate it to SQL query).

But why not just utilize your foreign keys if you have them?NavigationProperty to maneuver toEmployee and verify the value there?

var myQuery = from r in Role 
              where r.Employee.idEmployee == someId
              select r;
1
5/23/2017 12:20:13 PM

Popular Answer

Make sure to have the choice if you're utilizing EF 4.0 (.NET 4.0), a database-first programming model, and an EDMX model (visual designer).Include foreign key columns in the model When you include tables to your EDMX model, make sure:

enter image description here

If this option is not selected, EF 4.0 will function similarly to EF 1.0/3.5 (in.NET 3.5), including a navigation property but treating the foreign key column as a distinct column.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow