Reading foreign key data in Entity Framework
up vote
0
down vote
favorite
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
up vote
0
down vote
favorite
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
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
|
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
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