1 回答

TA贡献1860条经验 获得超9个赞
您不能完全删除外键,否则,您希望如何链接两个实体(即表)?相反,您可以做的是拥有一个可为空的 FK,这将有效地使关系为零或一到多。
在您的GameLevel课程中,将导航属性添加为以下内容的集合UserGameProfile:
public class GameLevel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public double PointsMin { get; set; }
public double PointsMax { get; set; }
public virtual ICollection<UserGameProfile> UserGameProfiles { get; set; }
}
然后在UserGameProfile类中,使属性可以为GameLevelId空:
public class UserGameProfile
{
// ...
// ...
public int? GameLevelId { get; set; }
[ForeignKey("GameLevelId")]
public virtual GameLevel GameLevel { get; set; }
}
这应该可以工作,甚至不必使用 Fluent API。
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报