[英]Entity Framework : Object is null but the foreign key isn't
我正在学习.NET Core和EF Core,但是我已经遇到了问题。
我是从头开始创建代码的,
这是我的代码:
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; }
}
当我尝试获取我的第一个“锻炼”时,他的“ MuscleId”为“ 1”,但他的“肌肉”为空...并且如果我尝试获取我的第一个“ Muscle”,则“锻炼”也为空...
我需要做些什么才能使这项工作吗? 谢谢
您的肌肉类主键应与运动类上的外键匹配。 像这样。
public class Muscle
{
[Key]
public int MuscleId { get; set; }
public string nom { get; set; }
public virtual ICollection<Exercice> Exercices { get; set; }
}
或使用[ForeignKey]属性。 你可以在这里阅读更多。 http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.