1

I am trying to build my first .NET app using Identity. It will be a basic Workout app, but I have been having trouble figuring out the Keys and ForeignKeys within Identity. Here is my ExerciseController:

 using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using WorkoutGenerator.Data.Migrations; namespace RandomWorkout.Models { public class Exercise { public string Name { get; set; } public string Description { get; set; } public MuscleGroup MuscleGroup { get; set; } public int MuscleGroupID { get; set; } [Key] public int ID { get; set; } [ForeignKey("ID")] public virtual ApplicationUser User { get; set; } public IList<ExerciseWorkout> ExerciseWorkouts { get; set; } = new List<ExerciseWorkout>(); } }` 

When I run the migration however, I get the error

They entity type 'MigrationOperation' requires a primary key to be defined.

I have run into this issue with other entities, in which case I just had to make a new builder.Entity in my DbContext class. With the MigrationOperation however, this doesn't seem to be the case. Am I completely on the wrong track of how to JOIN my table with the auto generated User table? Thanks

8
  • Have you tried using "Id" instead of "ID"? Commented Mar 6, 2018 at 4:43
  • @Marathon55 Just did. Same result unfortunately. I feel like joining my table to the AspNetUsers table is super simple and I'm just not seeing it Commented Mar 6, 2018 at 4:47
  • Would this be a many-to-many relationship? Commented Mar 6, 2018 at 11:07
  • @Marathon55 this one would be, yes. I will also be implementing a one to many to another of my tables (workouts). Should I have an intermediary take like I do for my own tables?(Its a ExerciseWorkout table). I've done both relationships on my own, I'm just not sure how to work it in with this Identity library Commented Mar 6, 2018 at 12:33
  • 1
    Yeah, that's right Commented Mar 6, 2018 at 21:33

2 Answers 2

1

Try this:

public class Exercise { public string Name { get; set; } public string Description { get; set; } public MuscleGroup MuscleGroup { get; set; } public int MuscleGroupID { get; set; } [Key] public int Id { get; set; } public int UserId {get; set;} [ForeignKey("UserId")] public virtual ApplicationUser User { get; set; } public IList<ExerciseWorkout> ExerciseWorkouts { get; set; } = new List<ExerciseWorkout>(); } 

Add a int for foreignkey and define an id property for primary key

Sign up to request clarification or add additional context in comments.

17 Comments

I am getting the same response of MigrationOperation Entity needs a primary key. Three questions that might narrow it down. 1. Do I need to add a .Entity<>()HasKey.() in my DbContext.cs like I did for my personal many-to-many relationship? 2. Do I need to add any code to the ApplicationUser.cs file? 3. Is Add-Migration not the correct command in the package console (and do I need to remove all data from my databases?)
So, i think the problem ist in entity excercise
Please , can you post your model?
Oh? Do you have any idea what it might be? Should I be douing something in the ApplicationUser entity? It is completely empty as of now, I wasn't sure if anything should be in it.
for this, i need review your model
|
0

for solved this problem It's better define id private the get by your Id

private Guid _id; [Key] public Guid ID { get { return _id; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.