Skip to main content
1 of 5

Code First Data Model

I am designing a new application to track sports statistics. I am using code first migration. I have the bare minimum POCO setup. The problem is I am not happy with the design. I'd greatly appreciate your comments and suggestions..

-Regards,

Ryan

This is my player account class

/// <summary>
/// Player account is a separate entity from the actual user account.
/// Player account allows a user to join teams and participate in a tournament.
/// </summary>
public class PlayerAccount
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }

    /// <summary>
    /// TournamentPlayerKeys tells us which team this player is competing with 
    /// in a certain tournament.
    /// </summary>
    public virtual ICollection<PlayerTeamKey> PlayerTeamKeys { get; set; }
    public virtual ICollection<PlayerMatchStat> PlayerMatchStats { get; set; }
}

then this is the team class

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// Team can be formed by a single user or a group of user. You'll need a team to join a tournament.
/// A single user can form his own team. 
/// </summary>
public class Team
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public virtual ICollection<PlayerTeamKey> PlayerTeamKeys { get; set; }
    public virtual ICollection<TeamTournamentKey> TeamTournamentKeys { get; set; }
}

Then I have the tournament class

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// Tournaments are basically a collection of games in one event. 
/// For example, Summer 2014 Basketball League.
/// </summary>
public class Tournament
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public virtual ICollection<TeamTournamentKey> TeamTournamentKeys { get; set; }
    public virtual ICollection<TournamentMatch> TournamentMatches { get; set; }
}

On a tournament there are multiple instances of matches (games)

/// <summary>
/// Match represents one occurence of a game in a league.
/// </summary>
public class TournamentMatch
{
    [Key]
    public int Id { get; set; }
    [Required]
    public int Tournament_Id { get; set; }

    [ForeignKey("Tournament_Id")]
    public virtual Tournament Tournament { get; set; }
    public virtual ICollection<TournamentMatchResult> TournamentMatchResults { get; set; }
}

and of course we have multiple instances of match results in one match. The Score property determines who is the winner.

/// <summary>
/// This is the result of a league match.
/// </summary>
public class TournamentMatchResult
{
    [Key]
    public int Id { get; set; }
    [Required]
    public int Match_Id { get; set; }
    [Required]
    public int Team_Id { get; set; }
    [Required]
    public double Score { get; set; }

    [ForeignKey("Match_Id")]
    public virtual TournamentMatch TournamentMatch { get; set; }
    [ForeignKey("Team_Id")]
    public virtual Team Team { get; set; }
    public virtual ICollection<PlayerMatchStat> PlayerMatchStat { get; set; }

}

I have a multipart key that creates the relationship between a player an a team

/// <summary>
/// This is a multipart key class that defines a player as part of a team.
/// Mind the naming convension, from smaller entity to a bigger entity; Player > Team.
/// </summary>
public class PlayerTeamKey
{

    [Required]
    public int PlayerAccount_Id { get; set; }
    [Required]
    public int Team_Id { get; set; }

    [ForeignKey("PlayerAccount_Id")]
    public virtual PlayerAccount Player { get; set; }
    [ForeignKey("Team_Id")]
    public virtual Team Team { get; set; }

}

And I have another like that for team and tournament

/// <summary>
/// This is a multipart key class that is a combination of a team id and a tournament id.
/// This basically tells us that the team with the given id is going to participating on a tournament with the given id.
/// </summary>
public class TeamTournamentKey
{
    [Required]
    public int Team_Id { get; set; }
    [Required]
    public int Tournament_Id { get; set; }

    [ForeignKey("Team_Id")]
    public virtual Team Team { get; set; }
    [ForeignKey("Tournament_Id")]
    public virtual Tournament Tournament { get; set; }

}