开发者

EF 4.1 Code First Mapping two columns in one table to list

I'm having an issue figuring out how to make this work for EF 4.1 Code First. I've looked around and found a similar problem, but I couldn't get it to work for me and it sounds like it didn't get answered for this person either.

Here is a simplified version of the two objects in question.

public class Team
{
  public int TeamId {get; set;}
  public virtual IList<Game> Games {get; set;}
}

public class Game
{
  public int GameId {get; set; }
  public int AwayTeamId {get; set;}
  public int HomeTeamId {get; set;}

  public virtual Team HomeTeam { get; set; }
  public virtual Team AwayTeam { get; set开发者_运维问答; }  
}

And here is my code for registering the FKs

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Game>()
        .HasRequired(a => a.HomeTeam)
        .WithMany()
        .HasForeignKey(u => u.HomeTeamId).WillCascadeOnDelete(false);

    modelBuilder.Entity<Game>()
        .HasRequired(a => a.AwayTeam)
        .WithMany()
        .HasForeignKey(u => u.AwayTeamId).WillCascadeOnDelete(false);
}

I want to bring back all games (home or away) that a team is in. Right now EF is creating a TeamId column in my database that never gets populated. If what I want is impossible, then I could do lists of HomeGames and AwayGames and another list of Games that is a combination of the two, but I'd like to try and avoid it if possible. I'm still learning this so any extra explanations or tips would be appreciated.


What you're asking of EF is a read only collection, i.e., an add operation would be meaningless for such a collection since EF wouldnt know into which table to insert, which I believe is unsupported.

Judging by your problem description, I would so something similar to your own suggestion and create a method such as GetGames() which would return just a unioned set of both home and away games. I think this is conceptually cleaner anyway.


EF is not able to map two relations into single navigation property. Your additional TeamId column is created because Games navigation property is considered as separate relation (your mapping says that neither HomeTeam or AwayTeam are part of that relation) so you instructed EF to create three relations for your.

You must define your model this way:

public class Team
{
  public int TeamId {get; set;}
  public virtual ICollection<Game> HomeGames { get; set; }
  public virtual ICollection<Game> AwayGames { get; set; }
}

And mapping must be:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Game>()
        .HasRequired(a => a.HomeTeam)
        .WithMany(t => t.HomeGames)
        .HasForeignKey(u => u.HomeTeamId).WillCascadeOnDelete(false);

    modelBuilder.Entity<Game>()
        .HasRequired(a => a.AwayTeam)
        .WithMany(t => t.AwayGames)
        .HasForeignKey(u => u.AwayTeamId).WillCascadeOnDelete(false);
}

Now if you want all team's games you can use simply:

var games = team.HomeGames.Concat(team.AwayGames);

You can wrap this into method returning IEnumerable or into property with just getter returning IEnumerable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜