Search code examples
nhibernatefluent-nhibernate

Nhibernate QueryOver does not work but CreateSqlQuery works


In my project I have a class named User with the following definition:

public class User
    {
        public virtual Guid UserId { get; set; }
        public virtual string UserName { get; set; }
        public virtual string Password { get; set; }
    }

and here is mapper class:

public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Id(x => x.UserId).Column("UserId");
            Map(x => x.UserName).Column("UserName");
            Map(x => x.Password).Column("Password");
        }
    }

when I try to execute the following code it does not return any result:

public IQueryable<User> GetByUserPassword(string userName, string password)
        {
            var result = Session.QueryOver<User>()
                .Where(x => x.Password == password && x.UserName == userName)
                .List<User>();
            return result.AsQueryable();
        }

But when I use CreateSqlQuery("select * from [dbo].[User]") method it returns value without column names:

result without column names

and here is my Nhibernate configuration code:

public class NHibernateSessionFactory
    {
        private ISessionFactory sessionFactory;

        private readonly string ConnectionString = "";       

        public NHibernateSessionFactory(String connectionString)
        {
            this.ConnectionString = connectionString;

        }

        public ISessionFactory SessionFactory
        {
            get { return sessionFactory ?? (sessionFactory = CreateSessionFactory()); }
        }

        public ISessionFactory CreateSessionFactory()
        {
            try
            {
                return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(x => x.FromConnectionStringWithKey(ConnectionString)))
               .Mappings(m =>
                         m.FluentMappings
                             .AddFromAssemblyOf<User>())
                .ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
                .BuildSessionFactory();
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }
    }

what is the problem?


Solution

  • Do not use QueryOver, use just Query:

    var result = Session.Query<User>()
                .Where(x => x.Password == password && x.UserName == userName);
    //already IQueryable<User>
    

    This was helpful but my mistake was a mistake in adding MapClass Assembly which was in other assembly:

    .AddFromAssemblyOf()