NHibernate.QueryException: could not resolve property

Mike's Avatar

Mike

12 Jul, 2010 12:59 PM via web

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 does this problem come from? NHibernate? The mappings? Linq To NHibernate?

Thanks in advance

Mike

  1. 2 Posted by Mike on 12 Jul, 2010 01:46 PM

    Mike's Avatar

    I've just updated my assemblies to use NHibernate 3.0, with the better LINQ provider.

    I still have the same exception, with a slighlty more useful message:

    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 joined the mappings files, hope this helps :)

  2. Support Staff 3 Posted by Paul Batum on 18 Jul, 2010 06:59 AM

    Paul Batum's Avatar

    NHibernate doesn't know about your Teams property. You told it about your
    "allteams" field but not the property. As such the LINQ provider has no data
    about that property and cannot generate the appropriate sql.

    If you need to use your Teams property in a linq query, i suggest you take a
    different approach:

           public class Player : BaseEntity<Player>
           {
                   private readonly IList<PlayerInTeam> teams = new
    List<PlayerInTeam>();

                   public IEnumerable<Team> Teams
                   {
                           get
                           {
                                   return teams;
                           }
                   }
           }

       public class PlayerMap : ClassMap<Player>
       {
           public PlayerMap()
           {
               HasMany<PlayerInTeam>(x => x.Teams)
                   .Inverse()
                   .Cascade.AllDeleteOrphan()
                   .Access.CamelCaseField()
                   .Where(" INSERT SQL HERE");
           }
       }

    The sql you provide in the where statement would be the sql equivalent of
    this:

                                              where playerInTeam.Roster.Match ==
    null

    I'm too lazy to write that out :)

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What comes next? 'Monday Tuesday Wednesday ?????'