views:

152

answers:

1

I have following classes in my domain model:

public class Player
{
    public virtual string ID { get; set; }
    public virtual string Name { get; set; }
    public virtual List<Match> Matches { get; set; }
}
public class Match
{
    public virtual int ID { get; set; }
    public virtual Player Player1 { get; set; }
    public virtual Player Player2 { get; set; }
}

As you can see a Player can play multiple matches, and every match has two players. How can I map these classes correctly using Fluent mapping?

+1  A: 

The players on the match mapping would be References (many-to-one) referencing different playerIds in the match table and the matches on player would be hasMany (one-to-many):

public sealed class PlayerMap : ClassMap<Player>
{
    public PlayerMap()
    {
     Id(x => x.ID).GeneratedBy.Native();
     Map(x => x.Name);
     HasMany(x => x.Matches).Cascade.SaveUpdate();
    }
}

public sealed class MatchMap : ClassMap<Match>
{
    public MatchMap()
    {
     Id(x => x.ID).GeneratedBy.Native();
     References(x => x.Player1, "player1_id").NotFound.Ignore().Cascade.None();
     References(x => x.Player2, "player2_id").NotFound.Ignore().Cascade.None();
    }
}
theGecko