Michel Andrade
Michel Andrade

Reputation: 4196

NHibernate Relations

I am working with NHibernate and I get this code below:

public class User
{
  public User()
  {
    public virtual int Id { get; set; }
    public virtual IList<UserConfirmation> UserConfirmation { get; set; }
    public virtual string Email;
  }

  public User()
  {
    UserConfirmation = new List<UserConfirmation>();
    }
}

public class UserConfirmation
{
  public virtual int Id { get; set; }
  public virtual User { get; set; }
}

public class UserMap :      ClassMap<User>
{
  public UserMap()        
  {
  Id(x => x.Id);            
  Map(x => x.Email);           
  HasMany(x => x.UserConfirmation)
    .Inverse()
    .Cascade.All();
  Table("user");
}

}

public class UserConfirmationMap : ClassMap<UserConfirmation>
{
    public UserConfirmationMap()
      {
        Id(x => x.Id);            
        References(x => x.User);
        Table("user_confirmation");
    }
}

But when I try to query over like this:

QueryOver<UserConfirmation>().Where(x => x.User.Email).Take(1).SingleOrDefault()

Than it says I do not have the property User.Email.

How can I solve this problem?

Upvotes: 0

Views: 140

Answers (4)

Diego Mijelshon
Diego Mijelshon

Reputation: 52725

You could use LINQ instead of QueryOver:

Query<UserConfirmation>().Where(x => x.User.Email).Take(1).SingleOrDefault()

BTW, .Take(1).SingleOrDefault() is probably not needed. If Email is unique, .SingleOrDefault() will be enough. Otherwise, you can use .FirstOrDefault()

Upvotes: 1

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

Problem is you are using x => x.User.Email this should be done with another alias, which would be user

UserConfirmation userConfirmationAlias;
User userAlias;


    QueryOver<UserConfirmation>(() => userConfirmationAlias)
    .joinAlias(() => userConfirmationAlias.User , () => userAlias)
    .Where(() => userAlias.Email).Take(1).SingleOrDefault()

something like above should do the trick

Upvotes: 2

Neelesh
Neelesh

Reputation: 496

Try doing:

IQueryOver<UserConfirmation,User> qo = _Session.QueryOver<UserConfirmation,User>().Where(x => x.User.Email).Take(1).SingleOrDefault();

Upvotes: 1

Zipper
Zipper

Reputation: 7204

I think you want something like this.

QueryOver<User>().Where(x => x.Email).Take(1).SingleOrDefault()

x should be a user already. If it's not I would use the intellisense to see what type it thinks it is.

Upvotes: 1

Related Questions