无法从相关表获取数据

时间:2022-09-02 20:52:50

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.

当我尝试访问每个披萨配料时,它总是说计数为0,尽管我的映射表有多个记录。

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.

这将返回0。

Controller:

控制器:

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

        return View(pizzaDetails);
    }

2 个解决方案

#1


1  

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.

看看这个:实体框架-加载相关实体。

#2


0  

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:

输出:

无法从相关表获取数据

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.

在看到您的代码之后,我可以建议您调查将Pizza对象传递给操作方法的源。披萨对象可能正在失去上下文(例如,您正在使用web服务来获取这个披萨对象),如果是这样,那么您可能会面临这样的问题。

#1


1  

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.

看看这个:实体框架-加载相关实体。

#2


0  

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:

输出:

无法从相关表获取数据

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.

在看到您的代码之后,我可以建议您调查将Pizza对象传递给操作方法的源。披萨对象可能正在失去上下文(例如,您正在使用web服务来获取这个披萨对象),如果是这样,那么您可能会面临这样的问题。