我刚开始使用EF代码,所以我在这个主题中完全是初学者.
我想在团队和比赛之间建立关系:1场比赛= 2队(主场,客场)和结果.我认为创建这样的模型很容易,所以我开始编码:
public class Team
{
[Key]
public int TeamId { get; set;}
public string Name { get; set; }
public virtual ICollection<Match> Matches { get; set; }
}
public class Match
{
[Key]
public int MatchId { get; set; }
[ForeignKey("HomeTeam"), Column(Order = 0)]
public int HomeTeamId { get; set; }
[ForeignKey("GuestTeam"), Column(Order = 1)]
public int GuestTeamId { get; set; }
public float HomePoints { get; set; }
public float GuestPoints { get; set; }
public DateTime …Run Code Online (Sandbox Code Playgroud) 首先使用 Entity Framework Core (5.0.1) 代码,我在实现一个对另一个类有两个引用的类时遇到问题。
这基本上是我想要的结构:
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Race
{
public int Id { get; set; }
public string Title { get; set; }
public int? StartLocationId { get; set; }
public Location StartLocation { get; set; }
public int? EndLocationId { get; set; }
public Location EndLocation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这实际上工作得很好,但当我实现表单来创建新的 Race 时,我想添加一些验证属性:
public class Race
{ …Run Code Online (Sandbox Code Playgroud)