相关疑难解决方法(0)

实体框架代码优先 - 来自同一个表的两个外键

我刚开始使用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)

c# orm entity-framework code-first entity-framework-4.1

252
推荐指数
6
解决办法
14万
查看次数

EF Core - 对同一个表的多个引用

首先使用 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)

database entity-framework-core .net-core asp.net-core

5
推荐指数
1
解决办法
7521
查看次数