gpt4 book ai didi

c# - EF Core 不完全引用模型之间

转载 作者:行者123 更新时间:2023-12-04 10:54:11 24 4
gpt4 key购买 nike

我在项目中使用的实体类:

    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;

}
}

我的 EF Core 上下文类:
public class ApplicationContext : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Game> Games { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<Stadium> Stadiums { get; set; }

public ApplicationContext()
{
Database.EnsureCreated();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=best-komp;Database=FootballApplicationDataBase;Trusted_Connection=True;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Player>()
.HasOne(p => p.Team)
.WithMany(t => t.Players)
.HasForeignKey(p => p.TeamId).OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(t => t.Id);
modelBuilder.Entity<Team>()
.HasMany(t => t.Players)
.WithOne(p => p.Team)
.HasForeignKey(p => p.Id).OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(t => t.Id);
modelBuilder.Entity<Game>()
.HasOne(g => g.HomeTeam)
.WithMany(t => t.HomeGames)
.HasForeignKey(t => t.HomeTeamId)
.HasPrincipalKey(t => t.Id);
modelBuilder.Entity<Game>()
.HasOne(g => g.AwayTeam)
.WithMany(t => t.AwayGames)
.HasForeignKey(t => t.AwayTeamId).OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(t => t.Id);
}
}

但是当我尝试调用我的上下文类时,比如
var db = new ApplicationContext();

我可以看到我的团队和玩家类不相互引用。
所有其他类都有引用团队,例如玩家有团队,游戏有团队。但是我的团队类(class)没有引用任何类(class),他们有 NULL 而不是引用。
问题是什么?

最佳答案

您的团队配置不正确。外键应该是 TeamId 而不是 Id。由于 Players 实体上的 FK 是 TeamId。

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

关于c# - EF Core 不完全引用模型之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59314023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com