I use Entity Data Model but I have aproblem: If a table e.g Customers have categoryId(foreign key from table category) it is not appear in cutomers entity???? so how can i retrive this value or set it?
4 Answers
The Entity Framework does render the actual property itself, it will create an property of Type Category. i.e.:
Customer.Category = new Category()
The Entity Framework will handle the references / foreign keys internally.

- 10,630
- 28
- 36
-
ok but I have another foreign key(category parentId) in category table(parentId foreign key from categoryid in the same table) how can I get this field? – Jul 28 '09 at 11:13
Colin's answer is 100% accurate and true - for the currently available release of the Entity Framework (with .NET 3.5 SP1).
For the upcoming and not-yet-released EF v2 (or EF 4??) that will ship with .NET 4.0 sometime in the future, the EF team has added what they call "foreign key associations", which in essence will allow you to just specify the foreign key value (e.g. CategoryID) instead of having to create / load / assign the whole associated entity.
See more in these excellent posts:
DISCLAIMER: this is pre-release information about a not-yet-released product and with no delivery date officially announced - you can download a Beta1 for now.
Marc

- 732,580
- 175
- 1,330
- 1,459
-
ok but I have another foreign key(category parentId) in category table(parentId foreign key from categoryid in the same table) how can I get this field? – Jul 28 '09 at 11:16
-
1You would then use Category.Category.Id (open the EF diagram and select the Category Entity. It should give you a list of all avaiable properties.) – Colin Jul 28 '09 at 11:38
-
@Wafaa: if you need to get at the ParentID in the referenced Category, then you'd have to load the whole Category entity, and then in that entity set the "ParentID" column – marc_s Jul 28 '09 at 11:40
-
And whne you have the diagram open you could choose to rename the parent Category Property (Which out of the box will probably have a name Like Category, or Category1) to CategoryParent or something similar – Colin Jul 28 '09 at 11:40
-
-
If I need to add new customer how can I set the value categoryId( foreign key column? – Jul 29 '09 at 14:22
IF you want to see the foreign key it self you must use .NET framework 4 not less in .NET 3.5 you'll find a reference to foreign key's class not the foreign key it self
Store s = new Store();
User u=s.User;//bring referance of the user with foreign key .net 3.5 & 4
int i = s.User_ID;//bring the user_id forein key .net 4

- 324
- 3
- 6