[英]One to many relationship with Web API put not working
I have a UserData class which holds all user information eg name, age, height, weight etc. I also have a WorkoutData class which holds all information of a gym workout eg date, workout duration, workout details, calories burned etc.我有一个 UserData 类,其中包含所有用户信息,例如姓名、年龄、身高、体重等。我还有一个 WorkoutData 类,其中包含健身房锻炼的所有信息,例如日期、锻炼持续时间、锻炼详细信息、燃烧的卡路里等。
A user can have many personal workouts associated with it, therefore it also has a List of WorkoutData which represents all of a users past workouts.一个用户可以有许多与之相关的个人锻炼,因此它还有一个 WorkoutData 列表,它代表所有用户过去的锻炼。
When a user adds(posts) a workout I will need the workout to be added to the List in the UserData object, so when I get a User it will show all of the users workouts from the list.当用户添加(发布)锻炼时,我需要将锻炼添加到 UserData 对象的列表中,因此当我获得用户时,它将显示列表中的所有用户锻炼。 Although I am not quite sure how to do this, or the correct way to do it.虽然我不太确定如何做到这一点,或者正确的方法来做到这一点。
UserData and WorkoutData:用户数据和锻炼数据:
namespace FitnessTracker
{
//holds all user information
public class UserData
{
//Upper bounds for BMI categories
const double SeverelyUnderweightUpper = 15.9;
const double UnderweightUpper = 18.4;
const double NormalWeightUpper = 24.9;
const double OverweightUpper = 29.9;
const double ModeratelyObeseUpper = 34.9;
public int ID { get; set; }
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Name")]
[StringLength(20, ErrorMessage = "First name cannot exceed 20 characters. ")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Second Name is required")]
[Display(Name = "Second Name")]
[StringLength(20, ErrorMessage = "Second name cannot exceed 20 characters. ")]
public string SecondName { get; set; }
public string Gender { get; set; }
[Range(5, 110, ErrorMessage = "Please enter a valid age. ")]
public int Age { get; set; }
[Display(Name = "Weight (KG)")]
[Range(5, 150, ErrorMessage = "KG must be between 5 and 150")]
public double WeightKG { get; set; }
[Display(Name = "Height (CM)")]
[Range(5, 220, ErrorMessage = "Height must be between 5 and 220 CM")]
public int HeightCM { get; set; }
//returns a value for user BMR
public double BMR
{
get
{
double bmr = (10 * WeightKG) + (6.25 * HeightCM) - (5 * Age) - 161;
return bmr;
}
}
//show current saved stats, allow to change to up-do-date stats - check BMR/BMI
//return a value for user BMI
public double BMIValue
{
get
{
double bmi = (WeightKG / HeightCM / HeightCM) * 10000;
return bmi;
}
}
public string BMICategory
{
get
{
double userBMI = this.BMIValue;
if(userBMI <= SeverelyUnderweightUpper)
{
return "Severely Underweight";
}
else if(userBMI <= UnderweightUpper)
{
return "Underweight";
}
else if(userBMI <= NormalWeightUpper)
{
return "Normal";
}
else if(userBMI <= OverweightUpper)
{
return "Overweight";
}
else if(userBMI <= ModeratelyObeseUpper)
{
return "Moderately Obese";
}
else
{
throw new Exception("BMI Category Error");
}
}
}
//Users list of personal workouts
public List<WorkoutData> Workouts { get; set; }
}
//holds all workout information such as start/end times, workout details, calories burned etc.
public class WorkoutData
{
public int ID { get; set; }
[Required(ErrorMessage = "Date of workout is required")]
public DateTime Date { get; set; }
[Display(Name = "Workout Length")]
public double WorkoutDuration { get; set; }
[Display(Name = "Workout Details")]
public string WorkoutDetails { get; set; }
[Display(Name = "Calories Burned")]
public int CaloriesBurned { get; set; }
public UserData User { get; set; }
}
}
My UserDataController get method (I need this to display all user information and all of a users workouts from the List):我的 UserDataController get 方法(我需要它来显示列表中的所有用户信息和所有用户锻炼):
[HttpGet]
public async Task<ActionResult<IEnumerable<UserData>>> GetUsers()
{
return await _context.Users.Include(r => r.Workouts).ToListAsync();
}
My WorkoutDataController post method:我的 WorkoutDataController 发布方法:
[HttpPost]
public async Task<ActionResult<WorkoutData>> PostWorkoutData(WorkoutData workoutData)
{
_context.Workouts.Add(workoutData);
await _context.SaveChangesAsync();
return CreatedAtAction("GetWorkoutData", new { id = workoutData.ID }, workoutData);
}
Can anyone tell me how to add the workout to the Users List in the above post method?谁能告诉我如何在上述帖子方法中将锻炼添加到用户列表中? Or if I should do it a different way please advise或者如果我应该以不同的方式做,请建议
You should add a foreign key property for the user to your WorkoutData class.您应该为用户添加一个外键属性到您的 WorkoutData 类。
public int UserID { get; set; }
Then when you POST the WorkoutData in your API action, explicitly set that UserID and EF will take care of the linking.然后,当您在 API 操作中发布 WorkoutData 时,明确设置 UserID 和 EF 将负责链接。 With that navigation link established, your Include clause in the GetUsers() method should properly include the relevant WorkoutData for each user.建立该导航链接后,GetUsers() 方法中的 Include 子句应正确包含每个用户的相关 WorkoutData。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.