我希望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");
}