Reading foreign key data in Entity Framework









up vote
0
down vote

favorite
1












I have two model classes, Chefs and Dishes. It is a 1-to-many relationship. One chef can make many dishes, but each dish can only be made by one chef. These are my model classes:



public class Chef

[Key]
public int ChefId get; set;
public string FirstName get; set;
public string LastName get; set;
public DateTime DoB get; set;
public List<Dish> Dishes get; set;


public class Dish

[Key]
public int DishId get; set;
public string DishName get; set;
public int Calories get; set;
public int Tastiness get; set;
public string Description get; set;
public int ChefId get; set;
public Chef Creator get; set;



When I click the dishes page, I am trying to display all the dishes with the name of the Chef who made that dish.



If I query like this:



var dishes = _context.Dishes.ToList (); 


I am only able to access the Id of the Chef, not the name.



I have also tried this:



var dishes = _context.Dishes.Include(c => c.Creator).ToList();


If I do that, Creator is the entire Chef object, and if I try to loop through it, I get errors as well. Can someone please get me in the right direction?



Thanks










share|improve this question























  • Please show your loop code so we can see the issue.
    – CodingYoshi
    Nov 11 at 3:05










  • foreach(Dish thisDish in dishes) string name = thisDish.Creator.FirstName; // etc
    – CodingYoshi
    Nov 11 at 3:12










  • @foreach (Dish dish in @ViewBag.dishes) <tr> <td>@dish.DishName</td> <td>@dish.ChefId</td> <td>@dish.Tastiness</td> <td>@dish.Calories</td> <td>@dish.Creator</td> </tr>
    – Faizan Rahman
    Nov 11 at 4:39










  • I have tried to do it in the way you specified, thisDish.Creator.FirstName
    – Faizan Rahman
    Nov 11 at 4:40










  • But it still gives me an error
    – Faizan Rahman
    Nov 11 at 4:40














up vote
0
down vote

favorite
1












I have two model classes, Chefs and Dishes. It is a 1-to-many relationship. One chef can make many dishes, but each dish can only be made by one chef. These are my model classes:



public class Chef

[Key]
public int ChefId get; set;
public string FirstName get; set;
public string LastName get; set;
public DateTime DoB get; set;
public List<Dish> Dishes get; set;


public class Dish

[Key]
public int DishId get; set;
public string DishName get; set;
public int Calories get; set;
public int Tastiness get; set;
public string Description get; set;
public int ChefId get; set;
public Chef Creator get; set;



When I click the dishes page, I am trying to display all the dishes with the name of the Chef who made that dish.



If I query like this:



var dishes = _context.Dishes.ToList (); 


I am only able to access the Id of the Chef, not the name.



I have also tried this:



var dishes = _context.Dishes.Include(c => c.Creator).ToList();


If I do that, Creator is the entire Chef object, and if I try to loop through it, I get errors as well. Can someone please get me in the right direction?



Thanks










share|improve this question























  • Please show your loop code so we can see the issue.
    – CodingYoshi
    Nov 11 at 3:05










  • foreach(Dish thisDish in dishes) string name = thisDish.Creator.FirstName; // etc
    – CodingYoshi
    Nov 11 at 3:12










  • @foreach (Dish dish in @ViewBag.dishes) <tr> <td>@dish.DishName</td> <td>@dish.ChefId</td> <td>@dish.Tastiness</td> <td>@dish.Calories</td> <td>@dish.Creator</td> </tr>
    – Faizan Rahman
    Nov 11 at 4:39










  • I have tried to do it in the way you specified, thisDish.Creator.FirstName
    – Faizan Rahman
    Nov 11 at 4:40










  • But it still gives me an error
    – Faizan Rahman
    Nov 11 at 4:40












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have two model classes, Chefs and Dishes. It is a 1-to-many relationship. One chef can make many dishes, but each dish can only be made by one chef. These are my model classes:



public class Chef

[Key]
public int ChefId get; set;
public string FirstName get; set;
public string LastName get; set;
public DateTime DoB get; set;
public List<Dish> Dishes get; set;


public class Dish

[Key]
public int DishId get; set;
public string DishName get; set;
public int Calories get; set;
public int Tastiness get; set;
public string Description get; set;
public int ChefId get; set;
public Chef Creator get; set;



When I click the dishes page, I am trying to display all the dishes with the name of the Chef who made that dish.



If I query like this:



var dishes = _context.Dishes.ToList (); 


I am only able to access the Id of the Chef, not the name.



I have also tried this:



var dishes = _context.Dishes.Include(c => c.Creator).ToList();


If I do that, Creator is the entire Chef object, and if I try to loop through it, I get errors as well. Can someone please get me in the right direction?



Thanks










share|improve this question















I have two model classes, Chefs and Dishes. It is a 1-to-many relationship. One chef can make many dishes, but each dish can only be made by one chef. These are my model classes:



public class Chef

[Key]
public int ChefId get; set;
public string FirstName get; set;
public string LastName get; set;
public DateTime DoB get; set;
public List<Dish> Dishes get; set;


public class Dish

[Key]
public int DishId get; set;
public string DishName get; set;
public int Calories get; set;
public int Tastiness get; set;
public string Description get; set;
public int ChefId get; set;
public Chef Creator get; set;



When I click the dishes page, I am trying to display all the dishes with the name of the Chef who made that dish.



If I query like this:



var dishes = _context.Dishes.ToList (); 


I am only able to access the Id of the Chef, not the name.



I have also tried this:



var dishes = _context.Dishes.Include(c => c.Creator).ToList();


If I do that, Creator is the entire Chef object, and if I try to loop through it, I get errors as well. Can someone please get me in the right direction?



Thanks







c# entity-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 8:35









marc_s

567k12710961246




567k12710961246










asked Nov 11 at 2:34









Faizan Rahman

91




91











  • Please show your loop code so we can see the issue.
    – CodingYoshi
    Nov 11 at 3:05










  • foreach(Dish thisDish in dishes) string name = thisDish.Creator.FirstName; // etc
    – CodingYoshi
    Nov 11 at 3:12










  • @foreach (Dish dish in @ViewBag.dishes) <tr> <td>@dish.DishName</td> <td>@dish.ChefId</td> <td>@dish.Tastiness</td> <td>@dish.Calories</td> <td>@dish.Creator</td> </tr>
    – Faizan Rahman
    Nov 11 at 4:39










  • I have tried to do it in the way you specified, thisDish.Creator.FirstName
    – Faizan Rahman
    Nov 11 at 4:40










  • But it still gives me an error
    – Faizan Rahman
    Nov 11 at 4:40
















  • Please show your loop code so we can see the issue.
    – CodingYoshi
    Nov 11 at 3:05










  • foreach(Dish thisDish in dishes) string name = thisDish.Creator.FirstName; // etc
    – CodingYoshi
    Nov 11 at 3:12










  • @foreach (Dish dish in @ViewBag.dishes) <tr> <td>@dish.DishName</td> <td>@dish.ChefId</td> <td>@dish.Tastiness</td> <td>@dish.Calories</td> <td>@dish.Creator</td> </tr>
    – Faizan Rahman
    Nov 11 at 4:39










  • I have tried to do it in the way you specified, thisDish.Creator.FirstName
    – Faizan Rahman
    Nov 11 at 4:40










  • But it still gives me an error
    – Faizan Rahman
    Nov 11 at 4:40















Please show your loop code so we can see the issue.
– CodingYoshi
Nov 11 at 3:05




Please show your loop code so we can see the issue.
– CodingYoshi
Nov 11 at 3:05












foreach(Dish thisDish in dishes) string name = thisDish.Creator.FirstName; // etc
– CodingYoshi
Nov 11 at 3:12




foreach(Dish thisDish in dishes) string name = thisDish.Creator.FirstName; // etc
– CodingYoshi
Nov 11 at 3:12












@foreach (Dish dish in @ViewBag.dishes) <tr> <td>@dish.DishName</td> <td>@dish.ChefId</td> <td>@dish.Tastiness</td> <td>@dish.Calories</td> <td>@dish.Creator</td> </tr>
– Faizan Rahman
Nov 11 at 4:39




@foreach (Dish dish in @ViewBag.dishes) <tr> <td>@dish.DishName</td> <td>@dish.ChefId</td> <td>@dish.Tastiness</td> <td>@dish.Calories</td> <td>@dish.Creator</td> </tr>
– Faizan Rahman
Nov 11 at 4:39












I have tried to do it in the way you specified, thisDish.Creator.FirstName
– Faizan Rahman
Nov 11 at 4:40




I have tried to do it in the way you specified, thisDish.Creator.FirstName
– Faizan Rahman
Nov 11 at 4:40












But it still gives me an error
– Faizan Rahman
Nov 11 at 4:40




But it still gives me an error
– Faizan Rahman
Nov 11 at 4:40

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245363%2freading-foreign-key-data-in-entity-framework%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245363%2freading-foreign-key-data-in-entity-framework%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線