I'm learning .NET Core and EF Core, but I already have an issue.
I created my code from scratch,
Here's my code :
public class DBContext : DbContext
{
public DbSet<Muscle> Muscle { get; set; }
public DbSet<Exercice> Exercice { get; set; }
}
public class Muscle
{
[Key]
public int id { get; set; }
public string nom { get; set; }
public virtual ICollection<Exercice> Exercices { get; set; }
}
public class Exercice
{
[Key]
public int id { get; set; }
public string libelle { get; set; }
public int MuscleId { get; set; }
public virtual Muscle Muscle { get; set; }
}
When I try to get my first "Exercice", his "MuscleId" is "1" but his "Muscle" is null... And if I try to get my first "Muscle", "Exercices" is null aswell ...
Is there something I have to do to make this work? Thanks
Your Muscle class primary key should match foreign key on Exercise class. Like this.
public class Muscle
{
[Key]
public int MuscleId { get; set; }
public string nom { get; set; }
public virtual ICollection<Exercice> Exercices { get; set; }
}
Or use [ForeignKey] Attribute. You can read more here. http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.