C# EF核心模型之间没有完整的参考

C# EF核心模型之间没有完整的参考,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,项目中使用的My entities类: public class Game { public int Id { get; set; } public int FirstTeamId { get; set; } public Team FirstTeam { get; set; } public int SecondTeamId { get; set; } public Team SecondTeam { get; set; }

项目中使用的My entities类:

    public class Game
    {
    public int Id { get; set; }

    public int FirstTeamId { get; set; }
    public Team FirstTeam { get; set; }
    public int SecondTeamId { get; set; }
    public Team SecondTeam { get; set; }

    public Stadium Stadium { get; set; }
    public DateTime Date { get; set; }

    public GameStatus Result { get; set; }

    public Game(DateTime date , GameStatus result )
    {
        Date = date;
        Result = result;
    }
}

public class Player
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime Birthday { get; set; }
    public PlayerStatus Status { get; set; }
    public PlayerHealthStatus HealthStatus { get; set; }
    public int Salary { get; set; }
    public int TeamId { get; set; }
    public Team Team { get; set; }

    public Player(string name , string surname, DateTime birthday, PlayerStatus status, PlayerHealthStatus healthStatus, int salary)
    {
        Name = name;
        Surname = surname;
        Birthday = birthday;
        Status = status;
        HealthStatus = healthStatus;
        Salary = salary;
    }

}

public class Stadium
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Capacity { get; set; }
    public int PriceForPlace { get; set; }

    public Stadium(string name, int capacity, int priceForPlace)
    {
        Name = name;
        Capacity = capacity;
        PriceForPlace = priceForPlace;
    }
}

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Player> Players { get; set; }

    public List<Game> Games { get; set; }

    public Team(string name)
    {
        Name = name;
    }

    public Team(string name, List<Player> players) : this(name)
    {
        Players = players;

    }
}
我可以看出,我的球队和球员级别并没有相互提及。 所有其他类都是指团队,比如玩家有团队,游戏有团队。但是我的团队类没有引用任何类,它们有NULL而不是一个引用。
有什么问题吗?

也许可以尝试使用ForeignKey注释。首先,添加using语句:

using System.ComponentModel.DataAnnotations.Schema;
然后在每个外键上方添加一个
[ForeignKey(“ModelName”)]
注释。例如:

[ForeignKey("FirstTeam")]
public int FirstTeamId { get; set; }
public Team FirstTeam { get; set; }
[ForeignKey("SecondTeam")]
public int SecondTeamId { get; set; }
public Team SecondTeam { get; set; }

这使得EF有时无法推断的外键关系变得明确。

您的团队配置不正确。外键应该是TeamId而不是Id。因为玩家身上的FK实体是TeamId

modelBuilder.Entity<Team>()
        .HasMany(t => t.Players)
        .WithOne(p => p.Team)
        .HasForeignKey(p => p.TeamId).OnDelete(DeleteBehavior.NoAction)
        .HasPrincipalKey(t => t.Id);
modelBuilder.Entity()
.HasMany(t=>t.Players)
.一人一组(p=>p.Team)
.HasForeignKey(p=>p.TeamId).OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(t=>t.Id);

您是否尝试在查询中使用include?比如:db.Teams.Include(t=>t.Players)@LuttiCoelho不起作用:(这对我没有帮助:(请参阅@Lutti Coelho的回答,我认为在onmodel创建覆盖中定义键时出错
[ForeignKey("FirstTeam")]
public int FirstTeamId { get; set; }
public Team FirstTeam { get; set; }
[ForeignKey("SecondTeam")]
public int SecondTeamId { get; set; }
public Team SecondTeam { get; set; }
modelBuilder.Entity<Team>()
        .HasMany(t => t.Players)
        .WithOne(p => p.Team)
        .HasForeignKey(p => p.TeamId).OnDelete(DeleteBehavior.NoAction)
        .HasPrincipalKey(t => t.Id);