0

after seeding data in my database this error occurs, its strange because I'm not adding any userexercises only basic exercises which are seeded already in database, please can someone help me?

I'm using EF Core 2.1

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TrainingExercises_UserExercises_UserExerciseID". The conflict occurred in database "WorkoutAppDB", table "dbo.UserExercises", column 'UserExerciseID'. The statement has been terminated.

modelBuilder.Entity<TrainingExercise>().HasData(
            new TrainingExercise { TrainingExerciseID = 1, TrainingID = 1, ExerciseID = 1 },
            new TrainingExercise { TrainingExerciseID = 2, TrainingID = 1, ExerciseID = 2 },
            new TrainingExercise { TrainingExerciseID = 3, TrainingID = 1, ExerciseID = 3 },
            new TrainingExercise { TrainingExerciseID = 4, TrainingID = 1, ExerciseID = 5 },
            new TrainingExercise { TrainingExerciseID = 5, TrainingID = 1, ExerciseID = 9 }
            );

UserExercise class

public class UserExercise
{
    [Key,ForeignKey("UserExerciseID")]
    public int UserExerciseID { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 2)]
    public string UserExerciseName { get; set; }

    public WorkoutUser WorkoutUser { get; set; }

    public ICollection<TrainingExercise> TrainingExercises { get; set; }
}

TrainingExercise class

public class TrainingExercise
{
    [Key,ForeignKey("TrainingExerciseID")]
    public int TrainingExerciseID { get; set; }

    public UserExercise UserExercise { get; set; }
    public int UserExerciseID { get; set; }

    public UserTraining UserTraining { get; set; }
    public int UserTrainingID { get; set; }

    public Training Training { get; set; }
    public int TrainingID { get; set; }

    public Exercise Exercise { get; set; }
    public int ExerciseID { get; set; }

    public ICollection<Workout> Workouts { get; set; }
    public ICollection<LogEntries> LogEntries { get; set; }
}

dbo.UserExercise

CREATE TABLE [dbo].[UserExercises] 
(
    [UserExerciseID]   INT            IDENTITY (1, 1) NOT NULL,
    [UserExerciseName] NVARCHAR (50)  NOT NULL,
    [WorkoutUserId]    NVARCHAR (450) NOT NULL,

    CONSTRAINT [PK_UserExercises] PRIMARY KEY CLUSTERED ([UserExerciseID] ASC),
    CONSTRAINT [FK_UserExercises_AspNetUsers_WorkoutUserId] 
        FOREIGN KEY ([WorkoutUserId]) REFERENCES [dbo].[AspNetUsers] ([Id])
);
GO

CREATE NONCLUSTERED INDEX [IX_UserExercises_WorkoutUserId]
ON [dbo].[UserExercises]([WorkoutUserId] ASC);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mathes
  • 7
  • 3
  • Are you passing a zero id into the update by chance? I have fallen foul of this many times – Programnik Nov 25 '19 at 05:11
  • Im not passing any id for this records, beacuse I want to them to stay null, only for trainingid and exerciseid – mathes Nov 25 '19 at 19:53

0 Answers0