我已经制作了一个1000行的种子代码,由于结构的原因,它不会起作用。
我有以下实体:
计划:是一个包含锻炼的锻炼计划。
锻炼:是一个包含集合列表的日常规。
设置:是y加载重复x次练习。
锻炼:是一项包含区域的健身运动。
区域:是人体上的一个区域,包含肌肉。
肌肉:是人体的肌肉。
以下是3个模型示例,在3个之间,我认为绑定出错了。
public class Workout
{
[Key]
public int WorkoutId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Set> Sets { get; set; }
}
public class Set
{
[Key]
public int SetId { get; set; }
//Foreign key for Exercise
public int ExerciseId { get; set; }
[ForeignKey("ExerciseId")]
public Exercise Exercise { get; set; }
public decimal Load { get; set; }
public decimal Order { get; set; }
}
public class Exercise
{
[Key]
public int ExerciseId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
//Foreign key for Region
public int RegionId { get; set; }
public Region Region { get; set; }
}
我做了一个长种子方法,填写了一个示例程序,所有区域和相关的肌肉。
我种植区域和 Mucles 的方式 - 工作正常
Regions = new List<Region>
{
new Region
{
RegionName = "Neck",
Muscles = new List<Muscle>
{
new Muscle {LatinName = "Omohyoid", DkName = "", EnName = "", Description = ""},
new Muscle {LatinName = "Sternohyoid", DkName = "", EnName = "", Description = ""},
//etc etc etc...
我种下练习的方式 - 工作正常
Exercises = new List<Exercise>
{
new Exercise
{
Name = "Benchpress",
Description = "Flat barbel",
Region = Regions.Find(x => x.RegionName == "Chest"),
但后来我想创建很多 Set ,实际的程序。
Sets = new List<Set>
{
//single Set
new Set
{
Exercise = Exercises.Find(x => x.Name == "Bænkpres"),
Load = 0.0M,
Order = 1.0M,
//etc etc..
然后我创建锻炼:
Workouts = new List<Workout>
{ //single Workout
new Workout
{
Name = "Chest", Description = "",
//Set list
Sets = Sets
我已经尝试了很多东西,结果出现以下错误:
System.Data.Entity.Infrastructure.DbUpdateException: Unable to determine
the principal end of the 'Gym.Models.Set_Exercise' relationship.
Multiple added entities may have the same primary key. ---> System.Data.Entity.Core.UpdateException:
Unable to determine the principal end of the 'Gym.Models.Set_Exercise'
relationship. Multiple added entities may have the same primary key.
我不确定从现在开始的去哪里,我真的很感激这项工作,而无需重写我的许多 Set 。
所以有人请指出我正确的方向。