'1 to 1 relationship does not work even when I define a shared primary key as well as foreign key
I'm trying to set up a 1 to 1 relationship between two classes:
public class Player
{
[Key]
public int PlayerId { get; set; }
public string PlayerName { get; set; }
public int PlayerRating { get; set; }
public int PlayerSkill { get; set; }
public virtual Record Record { get; set; }
}
public class Record
{
[Key]
[ForeignKey("Player")]
public int RecordId { get; set; }
public List<Player> RecordWins { get; set; }
public List<Player> RecordLosses { get; set; }
public virtual Player Player { get; set; }
}
public class DataContext : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Record> Records { get; set; }
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
}
According to this article, I must define a primary key on Player.cs and a primary and foreign key on Record.cs (which I did).
When I migrate this change using:
Add-migration AddPlayerRecordRelationship
It didn't work. It says:
"Unable to determine the relationship represented by navigation 'Player.Record' of type 'Record'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."
Is there anything I'm missing using this approach? I would appreciate any help. Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
