Как получить список элементов, включаемых в класс, из базы данных Entity Framework - PullRequest
0 голосов
/ 21 мая 2018

У меня проблема с моим приложением.Я не могу получить из базы данных объект, в котором есть список.Я действительно не очень хорош в Entity Framework, так что извините, если это действительно большая ошибка.

public List<Recipe> GetRecipes(Cook cook)
    {
        List<Recipe> L_Recipes = new List<Recipe>();
        Cook Cook = dbc.DbCook.SingleOrDefault(c => c.Nickname == cook.Nickname);
        L_Recipes = Cook.ListRecipes;
        return L_Recipes;
    }

, а это класс Recipe:

  public class Recipe
{
    //Attributes
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public float CostPrice { get; set; }
    public float SellingPrice { get; set; }
    public DateTime Date { get; set; }
    public List<Ingredient> ListIngredients { get; set; }
    public List<Comment> ListComments { get; set; }
    public Schedule Schedules { get; set; }
    //Builder
    public Recipe(string name, string type)
    {
        Name = name;
        Type = type;
        Date = DateTime.Now;
        ListIngredients = new List<Ingredient>();
        Schedules = new Schedule();
    }
    public Recipe(string name,string type,float costPrice,float SellingPrice,DateTime Date,List<Ingredient> ListIngredients,List<Comment> ListComments,Schedule Schedules)
    {
        this.Name = name;
        this.Type = type;
        this.CostPrice = costPrice;
        this.SellingPrice = SellingPrice;
        this.Date = Date;
        this.ListIngredients = ListIngredients;
        this.ListComments = ListComments;
        this.Schedules = Schedules;
    }
    public Recipe() { ListIngredients = new List<Ingredient>(); }
    //Methods
    public void AddIngredient(Ingredient i)
    {
        ListIngredients.Add(i);
    }
    public float CalculCostPrice()
    {
        float cost = 0;
        foreach (Ingredient ing in ListIngredients)
        {
            cost += ing.CalculCostIngredient();
        }
        return cost;
    }
    public float CalculSellingPrice()
    {
        return (float)(CalculCostPrice()*1.05);
    }
    public override string ToString()
    {
        string SP = string.Format("{0:00.00}", SellingPrice);
        return $"{Name} for {SP} euros added the {Date.ToString()}";
    }
}

Проблема в том, что яполучил информацию о рецепте, но не список в нем.

Это в ASP.NET

Включите код:

        public List<Recipe> GetRecipes(Cook cook)
    {
        List<Recipe> L_Recipes = new List<Recipe>();
        Cook Cook = dbc.DbCook.Include(c => c.ListRecipes).singleOrDefault(c => c.Nickname == cook.Nickname);
        L_Recipes = Cook.ListRecipes;
        return L_Recipes;
    }

1 Ответ

0 голосов
/ 21 мая 2018

Thx до https://stackoverflow.com/users/395675/philip-smith и https://stackoverflow.com/users/5748194/felipe-deveza Таким образом, ответом будет Использование Включить и добавить использование, включающее: public List<Recipe> GetRecipes(Cook cook) { List<Recipe> L_Recipes = new List<Recipe>(); Cook Cook = dbc.DbCook.Include(c => c.ListRecipes).singleOrDefault(c => c.Nickname == cook.Nickname); L_Recipes = Cook.ListRecipes; return L_Recipes; } и использование: using System.Data.Entity;

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...