Nhibernate QueryOver不起作用,但CreateSqlQuery工作

时间:2017-01-30 12:50:00

标签: nhibernate fluent-nhibernate

在我的项目中,我有一个名为User的类,其定义如下:

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

这里是mapper类:

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");
        }
    }

当我尝试执行以下代码时,它不会返回任何结果:

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

但是当我使用CreateSqlQuery(&#34; select * from [dbo]。[User]&#34;)方法时,它返回没有列名的值:

result without column names

这是我的Nhibernate配置代码:

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;
            }


        }
    }

问题是什么?

1 个答案:

答案 0 :(得分:2)

不要使用QueryOver,只使用Query:

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

这很有帮助,但我的错误是添加MapClass程序集中的错误,这是在其他程序集中:

.AddFromAssemblyOf()