使用导航属性而不是LINQ加入WebApi控制器

时间:2016-12-08 09:57:00

标签: c# entity-framework linq

我在Web Api控制器中使用Entity Framework Code First和LINQ to Entities。

我有3个与以下关系/导航属性相关的表:(模型类如下)

我的预订课程:

[Table("Reservations")] 
public class Reservation
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ReservationId { get; set; }
    public DateTime Date { get; set; }

    public int RoomId { get; set; }
    public Room Room { get; set; }



    public Customer Customer { get; set; }
    public int CustomerId { get; set; }

    // create association table ReservationMeals
    public virtual ICollection<ReservationMealItems> ReservationMeals { get; set; }
}

协会类:

 public class ReservationMealItems
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public int ReservationId { get; set; }

    public int MealId { get; set; }


    public virtual Reservation Reservation { get; set; }
    public virtual Meal Meal { get; set; }

}

用餐课程:

[Table("Meals")]
public class Meal
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string ImageUrl { get; set; }
    public int MealTypeId { get; set; }


    public virtual MealType Type { get; set; }

    public virtual ICollection<MenuMealItem> Menus { get; set; }

    // create association table ReservationMeal
    public virtual ICollection<ReservationMealItems> ReservationMeals { get; set; }

    // one-to-one with MealPrice where MealPrice is the dependant
    public virtual MealPrice MealPrice { get; set; }
}

这是我的控制器,我通过日期然后查询表格,以便从特定日期的所有预订中带回所有餐点。

        public IHttpActionResult GetReservationMealsForDate(DateDto date)
    {
        using (var context = new CafeteriaContext())
        {
            var reservationMeals = from p in context.Reservations
                                   where p.Date == date.Date
                                   join d in context.ReservationMeals
                                   on p.ReservationId equals d.ReservationId
                                   select new SimpleId
                                   {
                                       Id = d.MealId
                                   };


            var meals = from p in context.Meals
                        join d in reservationMeals
                        on p.Id equals d.Id
                        select new SimpleMeal
                        {
                            Name = p.Name
                        };


            return Ok(meals.ToList());
        }
    }

控制器工作并带回当天所有预订的膳食清单,但我确信它不是最佳的。

我尝试不使用JOIN,而是使用导航属性,但没有成功。我想知道关系是否设置正确,如果导航属性按原样放置,如果是,那么如何重写控制器以使用导航属性恢复相同的结果集。

作为一个额外的问题,我想知道如何使用LINQ方法语法编写正确的查询,因为我没有成功地编写它。

我目前正在学习,非常感谢任何解释。我已经阅读了这篇博客文章Coding Abel,从那里我设法让它发挥作用。但我仍然很想知道我的解决方案是多么正确,并渴望学习更好的替代方案。谢谢!

1 个答案:

答案 0 :(得分:3)

您可以尝试使用LINQ和方法Include急切加载。您的表ReservationMealItems与膳食和预约有关系,在这种情况下,您需要包括您的实体,如:

LAMBDA

var x = context.ReservationMealItems.Include(res => res.Reservation)
         .Where(dt => dt.Reservation.Date == DateTime.Now)
         .ThenInclude(meal => meal.Meal)
         .Select(d => d.Meal.Name)
         .ToList();

在这里您可以找到更多信息MSDN

查询语法

 var z = (from resevationMealsItems in context.ReservationMealItems
         join reservation in context.Reservation on resevationMealsItems.ReservationId equals
         reservation.ReservationId
         join meal in context.Meal on resevationMealsItems.MealId equals meal.Id
         select meal.Name).ToList();