I want to create class that contains multiple objects as a relation to another table.
For example, if I wanted to implement a team of three players using different objects instead of a collection, my code would've looked like this:
public class Team
{
[Key]
public int TeamId { get; set; }
public string Name { get; set; }
[ForeignKey("Goalkeeper")]
[Required]
public int GoalkeeperId { get; set; }
public virtual Player Goalkeeper { get; set; }
[ForeignKey("Defender")]
[Required]
public int DefenderId { get; set; }
public virtual Player Defender { get; set; }
[ForeignKey("Striker")]
[Required]
public int StrikerId { get; set; }
public virtual Player Striker { get; set; }
}
public class Player
{
[Key]
public int PlayerId { get; set; }
public string Name { get; set; }
[ForeignKey("Team")]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
}
Trying to create a migration based on this code I'm getting an error:
Unable to determine the relationship represented by navigation property 'Player.Team' of type 'Team'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
What am I missing? Is this even a correct approach to implement this?