0

我有 3 张表需要合并到一张表中。在下面你可以看到代码。

问题是每个球员都可以在每场比赛中参加一支或多支球队。每场新比赛的球员和球队安排。

那么我如何使用 fluent api 映射它,或者也许有更好的方法来解决它。从现在开始谢谢。

 public class Player
    {
        public int PlayerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
        public DateTime TimeStamp { get; set; }
        public ICollection<Team> Teams { get; set; }
        public ICollection<Tournament> Tournaments { get; set; }

    }

public class Team
    {
        public int TeamID { get; set; }
        public string Name { get; set; }
        public DateTime TimeStamp { get; set; }
        public ICollection<Player> Players { get; set; }
        public ICollection<Tournament> Tournaments { get; set; }
    }

public class Tournament
    {
        public int TournamentID { get; set; }
        public DateTime Date { get; set; }
        public int PlaceID { get; set; }
        public DateTime TimeStamp { get; set; }
        public virtual Place Place { get; set; }
        public ICollection<Player> Players { get; set; }
        public ICollection<Team> Teams { get; set; }
    }



  public class BPContext : DbContext
    {
        public DbSet<Tournament> Tournaments { get; set; }
        public DbSet<Place> Places { get; set; }
        public DbSet<Table> Tables { get; set; }
        public DbSet<Player> Players { get; set; }
        public DbSet<Team> Teams { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        }
    }
4

1 回答 1

1

我看不出你想用这个来完成什么。您的表格包含所有玩家、所有球队和所有锦标赛。

我想会有一场比赛要打?您可以做的是创建另一个表匹配,以使用与此类似的实体:

public class Match
{

 [Key]
 public int MatchId {get;set;}

 [ForeignKey("Tournament")]
 public int TournamentId {get;set;}

 [InverseProperty("Matches")]
 public virtual List<Team> Teams {get;set;}

 [InverseProperty("Matches")]
 public virtual List<Player> Players {get;set;}

 [InverseProperty("Matches")]
 public virtual Tournament Tournament {get;set;}
}

这个新实体拥有所有 3 个之前的实体。但是,您必须修改以前的内容以包含这些更改:

public class Player
{
    public int PlayerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public DateTime TimeStamp { get; set; }

    [InverseProperty("Players")]
    public virtual List<Match> Matches { get; set; }

    [InverseProperty("Players")]
    public virtual List<Team> Teams {get;set;}
}

可以使用 LINQ 为玩家获取所有锦标赛:ctx.Players.Where(x => x.PlayerId == 15).Matches.Select(x => x.TournamentId)。

如果您想查看锦标赛中的所有玩家:ctx.Matches.Where(x => x.TournamentId ==15).Players.Select(x => x.Name)。

public class Team
{
    public int TeamID { get; set; }
    public string Name { get; set; }
    public DateTime TimeStamp { get; set; }

    [InverseProperty("Teams")]
    public List<Match> Matches {get;set;}

    [InverseProperty("Teams")]
    public List<Player> Players {get;set;}
}

public class Tournament
{
    public int TournamentID { get; set; }
    public DateTime Date { get; set; }
    public int PlaceID { get; set; }
    public DateTime TimeStamp { get; set; }
    public virtual Place Place { get; set; }

    [InverseProperty("Tournament")]
    public virtual List<Match> Matches {get;set;}
}
于 2012-10-02T05:19:17.523 回答