Reading foreign key data in Entity Framework
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
|
show 4 more comments
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
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
|
show 4 more comments
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
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
c# entity-framework
edited Nov 11 at 8:35
marc_s
569k12811001250
569k12811001250
asked Nov 11 at 2:34
Faizan Rahman
141
141
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
|
show 4 more comments
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
|
show 4 more comments
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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