0

我有3個表格需要在一個表格中進行組合。在下面你可以看到代碼。EF 3個表格上的多對多關係(流利API)

問題是每個玩家都可以在每個錦標賽上的一個或多個團隊中玩。每個新比賽的球員和球隊安排。

那麼我怎樣才能將它與流利的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>(); 

     } 
    } 
+0

你爲什麼要把所有東西合併成一張表? –

回答

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個以前的實體。然而,你必須修改以前的包含這些改動:ctx.Players.Where(X => x.PlayerId == 15).Matches.Select:

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來完成(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;} 
}