NHibernate Relations

Go To StackoverFlow.com

0

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?

2012-04-04 01:26
by Michel Andrade


2

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

2012-04-04 04:53
by Low Flying Pelican
Peferct! I just modify to:

QueryOver() .JoinQueryOver(x => x.User)
.Take(1) .SingleOrDefault() - Michel Andrade 2012-04-04 10:54



1

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.

2012-04-04 01:38
by Zipper
I do not think so, beacause the problem is about do not find the property User.Email - Michel Andrade 2012-04-04 02:42
What type does Visual Studio think "x" is in the QueryOver( - Zipper 2012-04-04 03:43


1

Try doing:

IQueryOver<UserConfirmation,User> qo = _Session.QueryOver<UserConfirmation,User>().Where(x => x.User.Email).Take(1).SingleOrDefault();
2012-04-04 04:53
by Neelesh


1

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()

2012-04-04 13:14
by Diego Mijelshon
Ads