ApplicationUser中的模型列表未在ApplicationDbContext中加载

时间:2019-11-28 10:55:58

标签: c# asp.net asp.net-mvc model-view-controller

我希望ApplicationUser吃多种类型的食物,这些食物也可以被更多的用户食用。 我在ApplicationUser中创建了模型Food的列表,并添加了迁移,但是我无法在数据库中看到list属性,也无法在add food actionresult中对其进行应有的更新。

public class ApplicationUser : IdentityUser
    {...

         public List<Food> FoodAte { get; set; }

}
        [HttpPut]
        public ActionResult AddFood(int id) //displays all the food so the user can choose what he ate
        {

            ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());


            Food food = db.Foods.Find(id);

            user.FoodAte.Add(food);
            user.Calories -= food.Calories;

            user.Carbs -= (int)((food.Carbs)*food.Quantity);
            user.Proteins -= (int)((food.Proteins) * food.Quantity);
            user.Fats -= (int)((food.Fats) * food.Quantity);

            db.SaveChanges();


            return View("FoodAte");
        }

0 个答案:

没有答案