Hello all,
I'm using FluentNHibernate and Linq To Nhibernate, with these entities (only the relevant parts):
public class Player : BaseEntity<Player>
{
private readonly IList<PlayerInTeam> allTeams = new List<PlayerInTeam>();
public IEnumerable<Team> Teams
{
get
{
return from playerInTeam in allTeams
where playerInTeam.Roster.Match == null
select playerInTeam.Roster.Team;
}
}
}
public class PlayerInTeam : BaseEntity<PlayerInTeam>
{
public int PlayerNumber { get; set; }
public Player Player { get; set; }
public Position Position { get; set; }
public Roster Roster { get; set; }
}
public class Roster : BaseEntity<Roster>
{
private readonly IList<PlayerInTeam> players = new List<PlayerInTeam>();
public Team Team { get; set; }
public IEnumerable<PlayerInTeam> Players { get { return players; } }
}
public class Team : BaseEntity<Team>
{
private readonly IList<Roster> allRosters = new List<Roster>();
public Team(string name, Sex sex)
{
allRosters.Add(new Roster(this));
}
public Roster DefaultRoster
{
get { return allRosters.Where(r => r.Match == null).First(); }
}
}
and the matching mappings:
public class PlayerMap : ClassMap<Player>
{
public PlayerMap()
{
HasMany<PlayerInTeam>(Reveal.Member<Player>("allTeams"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
public class PlayerInTeamMap : ClassMap<PlayerInTeam>
{
public PlayerInTeamMap()
{
References(pit => pit.Player)
.Not.Nullable();
References(pit => pit.Roster)
.Not.Nullable();
}
}
public class RosterMap : ClassMap<Roster>
{
public RosterMap()
{
References(tr => tr.Team)
.Not.Nullable();
HasMany<PlayerInTeam>(Reveal.Member<Roster>("players"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
public class TeamMap : ClassMap<Team>
{
public TeamMap()
{
HasMany<Roster>(Reveal.Member<Team>("allRosters"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
I have this repository method:
public IEnumerable<Player> PlayersNotInTeam(Team team)
{
return from player in Session.Linq<Player>()
where !player.Teams.Contains(team)
select player;
}
Which gives me this exception: NHibernate.QueryException: could not resolve property: Teams of: Emidee.CommonEntities.Player [.Where(NHibernate.Linq.NhQueryable`1[Emidee.CommonEntities.Player], Quote((player, ) => (Not(.Contains(player.Teams, p1, )))), )]
I've looked inside the hbm file, generated by Fluent NHibernate, concerning Player, and here is what I get:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Emidee.CommonEntities.Player, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" table="Players">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="increment" />
</id>
<bag access="field.camelcase" cascade="all-delete-orphan" inverse="true" name="allTeams" mutable="true">
<key>
<column name="Player_id" not-null="true" />
</key>
<one-to-many class="Emidee.CommonEntities.PlayerInTeam, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" />
</bag>
</class>
</hibernate-mapping>
Indeed, I don't have a Teams property for Player in that file.
But shouldn't my mapping take care of this? Where do you think the problem is?
Thanks in advance
Mike