nickp90
nickp90

Reputation: 81

Can't get data from related table

I have two models, Pizza and Topping. Each pizza can have many toppings.

When I try to access each Pizzas toppings it always says the count is 0, despite my mapping table having multiple records.

However, I can get Pizzas which are related to Toppings (which I dont want to do - i guess its the wrong way round for some reason).

Models:

public class Pizza
{
    public int PizzaId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Size { get; set; }
    public string Status { get; set; }
    public virtual ICollection<Topping> PizzaToppings { get; set; }
}

public class Topping
{
    public int ToppingId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Size { get; set; }
    public string Status { get; set; }

    public virtual ICollection<Pizza> PizzasOn { get; set; }
}

public class EFDbContext : DbContext
{
    public DbSet<Pizza> Pizzas { get; set; }
    public DbSet<Topping> Toppings { get; set; }
}

View:

<p>Pizza Topping Count: @Model.PizzaToppings.Count()</p>

This returns 0.

Controller:

public ViewResult ViewPizza(Pizza pizza)
    {
        Pizza pizzaDetails = pizza;

        return View(pizzaDetails);
    }

Upvotes: 2

Views: 126

Answers (2)

Sayan Pal
Sayan Pal

Reputation: 4946

I have tested your model in console application it is working fine. Below is my code:

        PizzaContext context = new PizzaContext();

        Pizza veggiDelite = new Pizza { Name = "Veggi Delite" };
        Topping pineapple = new Topping { Name = "Pineapple" };
        Topping chicken = new Topping { Name = "Chicken" };
        Pizza supremeChickenPizza = new Pizza { Name = "Supreme Chicken Pizza" };

        veggiDelite.PizzaToppings = new Collection<Topping> { new Topping { Name = "Jalepeeno" }, pineapple };

        supremeChickenPizza.PizzaToppings = new Collection<Topping> { pineapple };

        chicken.PizzasOn = new Collection<Pizza> { supremeChickenPizza };

        context.Pizzas.Add(veggiDelite);

        context.Toppings.Add(chicken);

        context.SaveChanges();

        foreach (Topping topping in context.Pizzas.SelectMany(p => p.PizzaToppings))
        {
            Console.WriteLine(topping.Name);
        }

        Console.ReadLine();

Output:

Output

After seeing your code, what I can suggest is to investigate the source from which you are passing the Pizza object to your action method. It is possible that the pizza object is loosing the context (for example you are using a web service to get this Pizza object) and if that the case then one may face this kind of issue.

Upvotes: 0

germi
germi

Reputation: 4658

You will have to explicitly load the Toppings like so:

var pizzas = db.Pizzas.Include("PizzaToppings");
var pizza = pizzas.Where()... // select your specific pizza

Have a look at this: Entity Framework - Loading Related Entities.

Upvotes: 1

Related Questions