Home > OS >  EF Core Foreign Key configuration problem Code First Problem
EF Core Foreign Key configuration problem Code First Problem

Time:11-23

I have 4 tables in the app.

  • User table (from the IdentityUser) - UserId is the PK
  • A Club table: PK is club_id.
  • A Team table which must belong to a Club. The PK being team_id club_id.
  • A table Player which is a user (PK UserID).
  • A table Team_Players which must belong to a team: the PK being team_id club_id UserId.

When I try to add a migration I get the following error:

The relationship from 'Team_Players.Team' to 'Team' with foreign key properties {'Team_Id' : string} cannot target the primary key {'Team_Id' : string, 'Club_Id' : string} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.

Thank you very much in advance for the help.

Here is the code for the classes:

public class Club : BaseModel
{
        [Display(Name = "ID")]
        [Required]
        [Key]
        public string Club_id { get; set; }
}

public class Team : BaseModel
{
        [Display(Name = "ID")]
        [Required]
        public string Team_Id { get; set; }

        [ForeignKey("Club_Id")]
        public Club Club { get; set; }

        [Required]
        [Display(Name = "Club")]
        [MaxLength(50, ErrorMessage = "Field cannot be more than 50 Characters")]
        public string Club_Id  { get; set; }
}    

public class Player : BaseModel
{
        [Required]
        [Display(Name = "ID")]
        public string UserId { get; set; }
     
        [ForeignKey("Club_Id")]
        public Team Club { get; set; }

        [Required]
        [Display(Name = "Club ID")]
        [MaxLength(50, ErrorMessage = "Field cannot be more than 50 Characters")]
        public string Club_Id { get; set; }
}

public class Team_Players:BaseModel
{
        [Required]
        [Display(Name = "ID")]
        public string UserId { get; set; }

        [ForeignKey("Club_Id")]
        public Club Club { get; set; }

        [Display(Name = "Club ID")]
        [MaxLength(50, ErrorMessage = "Field cannot be more than 50 Characters")]
        public string Club_Id { get; set; }

        [ForeignKey("Team_Id")]
        public Team Team { get; set; }
        [Display(Name = "Team ID")]
        public string Team_Id { get; set; }
}

And finally my OnModelCreating method has the following code:

modelBuilder.Entity<Team>()
                .HasKey(e => new { e.Team_Id, e.Club_Id});

modelBuilder.Entity<Coach>()
                .HasKey(e => new { e.UserId, e.Club_Id });

modelBuilder.Entity<Player>()
               .HasKey(e => new { e.UserId, e.Club_Id });

modelBuilder.Entity<Team_Players>()
               .HasKey(e => new { e.UserId, e.Club_Id ,e.Team_Id});

CodePudding user response:

A Foreign Key targets a Key, not just part of a Key. So

[ForeignKey("Team_Id")]
public Team Team { get; set; }

should be

[ForeignKey("Team_Id, Club_Id")]
public Team Team { get; set; }

since

Entity<Team>().HasKey(e => new { e.Team_Id, e.Club_Id});
  • Related