Nhibernate QueryOver 不起作用,但 CreateSqlQuery 起作用
Nhibernate QueryOver does not work but CreateSqlQuery works
在我的项目中,我有一个名为 class 的用户,其定义如下:
public class User
{
public virtual Guid UserId { get; set; }
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
}
这里是映射器 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");
}
}
当我尝试执行以下代码时,它没有 return 任何结果:
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("select * from [dbo].[User]") 方法时它 returns 没有列名的值:
这是我的 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;
}
}
}
有什么问题?
不要使用 QueryOver,只使用 Query:
var result = Session.Query<User>()
.Where(x => x.Password == password && x.UserName == userName);
//already IQueryable<User>
这很有帮助,但我的错误是在添加其他程序集中的 MapClass 程序集时出错:
.AddFromAssemblyOf()
在我的项目中,我有一个名为 class 的用户,其定义如下:
public class User
{
public virtual Guid UserId { get; set; }
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
}
这里是映射器 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");
}
}
当我尝试执行以下代码时,它没有 return 任何结果:
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("select * from [dbo].[User]") 方法时它 returns 没有列名的值:
这是我的 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;
}
}
}
有什么问题?
不要使用 QueryOver,只使用 Query:
var result = Session.Query<User>()
.Where(x => x.Password == password && x.UserName == userName);
//already IQueryable<User>
这很有帮助,但我的错误是在添加其他程序集中的 MapClass 程序集时出错:
.AddFromAssemblyOf()