Skip to main content
etiquetas editadas
Enlace
gbianchi
  • 20.6k
  • 13
  • 40
  • 60
se añadieron 5024 caracteres en el cuerpo
Origen Enlace
[HttpGet("GetByRoster""GetPlayersByRoster/{championshipId}/{teamId}")]
    public async Task<IEnumerable<Player>>Task<IEnumerable<Roster>> GetByRostersGetPlayersByRoster(long championshipId, long teamId)
    {
        var playersroster = await _context.PlayersRosters.Include(pr => pr.RostersPlayer)
                                .ThenIncludeInclude(r => r.Championship)
                                .Include(pr => pr.RostersTeam)
                                .ThenIncludeWhere(r => r.TeamChampionshipId == championshipId && r.TeamId == teamId)
                                .ToListAsync(); 

        return roster;
    }

Estas son las clases involucradas.

public class Championship
    {
        [Key]
        public long Id { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Identifier { get; set; }

        [Required]
        public DateTime StartDate { get; set; }

        public DateTime? EndDate { get; set; }

        [Required]
        public required bool InProgress { get; set; }

        public long? FederationId { get; set; }

        public long? AssociationId { get; set; }

        public long? LeagueId { get; set; }

        public required long CategoryId { get; set; }

        [ForeignKey("FederationId")]
        public Federation? Federation { get; set; }

        [ForeignKey("AssociationId")]
        public Association? Association { get; set; }

        [ForeignKey("LeagueId")]
        public League? League { get; set; }

        [ForeignKey("CategoryId")]
        public Category? Category { get; set; }

        public List<Roster>? Rosters { get; set; }

        public List<ChampionshipSponsor>? ChampionshipSponsors { get; set; }

        public List<ChampionshipCondition>? ChampionshipConditions { get; set; }

        public List<ChampionshipTeam>? ChampionshipTeams { get; set; }

        public DateTime? CreatedAt { get; set; }

        public DateTime? UpdatedAt { get; set; }
    }

public class Roster
    {
        [Key]
        public long Id { get; set; }

        [Required]
        public DateTime SaveDate { get; set; }

        [Required]
        public bool IsActive { get; set; }

        [Required]
        [MaxLength(15)]
        public required string UniformNumber { get; set; }

        [Required]
        public long ChampionshipId { get; set; }

        [Required]
        public long TeamId { get; set; }

        [Required]
        public long PlayerId { get; set; }

        [ForeignKey("ChampionshipId")]
        public Championship? Championship { get; set; }

        [ForeignKey("TeamId")]
        public Team? Team { get; set; }

        [ForeignKey("PlayerId")]
        public Player? Player { get; set; }

        public List<OffensiveStat>? OffensiveStats { get; set; }

        public List<PitchingStat>? PitchingStats { get; set; }

        public DateTime CreatedAt { get; set; }

        public DateTime UpdatedAt { get; set; }
    }

public class Player
    {
        [Key]
        public long Id { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Dni { get; set; }

        [Required]
        [MaxLength(200)]
        public required string FirstName { get; set; }

        [Required]
        [MaxLength(200)]
        public required string SecondName { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Surname { get; set; }

        [Required]
        [MaxLength(200)]
        public required string SecondSurname { get; set; }

        [Required]
        public DateTime BirthDate { get; set; }

        [Required]
        [MaxLength(10)]
        public required string Gender { get; set; }

        public List<Roster>? Rosters { get; set; }

        public DateTime CreatedAt { get; set; }

        public DateTime UpdatedAt { get; set; }
    }

public class Team
    {
        [Key]
        public long Id { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Name { get; set; }

        public List<Roster>? Rosters { get; set; }

        public List<AssociationTeam>? AssociationsTeams { get; set; }

        public List<TeamCategory>? TeamCategories { get; set; }

        public List<FederationTeam>? FederationTeams { get; set; }

        public List<LeagueTeam>? LeagueTeams { get; set; }

        public List<ChampionshipTeam>? ChampionshipTeams { get; set; }

        public DateTime CreatedAt { get; set; }

        public DateTime UpdatedAt { get; set; }
    }

Este es el erro que me arroja cuando intento hacer la consulta

introducir la descripción de la imagen aquí

[HttpGet("GetByRoster")]
 public async Task<IEnumerable<Player>> GetByRosters()
   {
      var players = await _context.Players.Include(p => p.Rosters)
                    .ThenInclude(r => r.Championship)
                    .Include(p => p.Rosters)
                    .ThenInclude(r => r.Team)
                    .ToListAsync();
   }
[HttpGet("GetPlayersByRoster/{championshipId}/{teamId}")]
    public async Task<IEnumerable<Roster>> GetPlayersByRoster(long championshipId, long teamId)
    {
        var roster = await _context.Rosters.Include(r => r.Player)
                                .Include(r => r.Championship)
                                .Include(r => r.Team)
                                .Where(r => r.ChampionshipId == championshipId && r.TeamId == teamId)
                                .ToListAsync(); 

        return roster;
    }

Estas son las clases involucradas.

public class Championship
    {
        [Key]
        public long Id { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Identifier { get; set; }

        [Required]
        public DateTime StartDate { get; set; }

        public DateTime? EndDate { get; set; }

        [Required]
        public required bool InProgress { get; set; }

        public long? FederationId { get; set; }

        public long? AssociationId { get; set; }

        public long? LeagueId { get; set; }

        public required long CategoryId { get; set; }

        [ForeignKey("FederationId")]
        public Federation? Federation { get; set; }

        [ForeignKey("AssociationId")]
        public Association? Association { get; set; }

        [ForeignKey("LeagueId")]
        public League? League { get; set; }

        [ForeignKey("CategoryId")]
        public Category? Category { get; set; }

        public List<Roster>? Rosters { get; set; }

        public List<ChampionshipSponsor>? ChampionshipSponsors { get; set; }

        public List<ChampionshipCondition>? ChampionshipConditions { get; set; }

        public List<ChampionshipTeam>? ChampionshipTeams { get; set; }

        public DateTime? CreatedAt { get; set; }

        public DateTime? UpdatedAt { get; set; }
    }

public class Roster
    {
        [Key]
        public long Id { get; set; }

        [Required]
        public DateTime SaveDate { get; set; }

        [Required]
        public bool IsActive { get; set; }

        [Required]
        [MaxLength(15)]
        public required string UniformNumber { get; set; }

        [Required]
        public long ChampionshipId { get; set; }

        [Required]
        public long TeamId { get; set; }

        [Required]
        public long PlayerId { get; set; }

        [ForeignKey("ChampionshipId")]
        public Championship? Championship { get; set; }

        [ForeignKey("TeamId")]
        public Team? Team { get; set; }

        [ForeignKey("PlayerId")]
        public Player? Player { get; set; }

        public List<OffensiveStat>? OffensiveStats { get; set; }

        public List<PitchingStat>? PitchingStats { get; set; }

        public DateTime CreatedAt { get; set; }

        public DateTime UpdatedAt { get; set; }
    }

public class Player
    {
        [Key]
        public long Id { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Dni { get; set; }

        [Required]
        [MaxLength(200)]
        public required string FirstName { get; set; }

        [Required]
        [MaxLength(200)]
        public required string SecondName { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Surname { get; set; }

        [Required]
        [MaxLength(200)]
        public required string SecondSurname { get; set; }

        [Required]
        public DateTime BirthDate { get; set; }

        [Required]
        [MaxLength(10)]
        public required string Gender { get; set; }

        public List<Roster>? Rosters { get; set; }

        public DateTime CreatedAt { get; set; }

        public DateTime UpdatedAt { get; set; }
    }

public class Team
    {
        [Key]
        public long Id { get; set; }

        [Required]
        [MaxLength(200)]
        public required string Name { get; set; }

        public List<Roster>? Rosters { get; set; }

        public List<AssociationTeam>? AssociationsTeams { get; set; }

        public List<TeamCategory>? TeamCategories { get; set; }

        public List<FederationTeam>? FederationTeams { get; set; }

        public List<LeagueTeam>? LeagueTeams { get; set; }

        public List<ChampionshipTeam>? ChampionshipTeams { get; set; }

        public DateTime CreatedAt { get; set; }

        public DateTime UpdatedAt { get; set; }
    }

Este es el erro que me arroja cuando intento hacer la consulta

introducir la descripción de la imagen aquí

Origen Enlace

¿Cómo puedo hacer consulta a varias tablas en .NET8?

Buen día a todos.

Como dice el título, estoy intentando consultar los datos de varias tablas que están relacionadas a través de una tabla pivote, tengo las tablas jugadores, equipos y campeonatos y estas se relacionan a través de una tabla llamada rosters, la cuestión es que estoy intentando obtener los datos de cada una de las tablas pero no logro hacer correctamente los join entre ellas a través de la tabla rosters.

Este es el código del método que estoy utilizando.

[HttpGet("GetByRoster")]
 public async Task<IEnumerable<Player>> GetByRosters()
   {
      var players = await _context.Players.Include(p => p.Rosters)
                    .ThenInclude(r => r.Championship)
                    .Include(p => p.Rosters)
                    .ThenInclude(r => r.Team)
                    .ToListAsync();
   }