How to make a Nhibernate Case query using QueryOver?

I have a simple database with a user table, it has a simple administrator with

UserName = "Admin"
Password = "admins"

I am using NHibernate to query this table in the login form. Suppose that the login form is inserted with UserName="ADMIN" and password="ADMIN" as in uppercase.

The system should not allow login. However, when I use such a query,

 using (var session = NhibernateHelper.OpenSession()) { return new List<User> (session.QueryOver<User>() .Where(u => u.UserName == userName) .And(u => u.Password == password) .Future()); }} 

The system ignores case sensitivity and selects the user. So how can I make the request sensitive <? >

0
source share
4 answers

We can directly specify COLLATE as part of the SQL column evaluation

 session .QueryOver<User>() // expecting that user name could be any case // if not, we can use the same as for password below .Where(u => u.UserName == userName) // instead of this //.And(u => u.Password == password) .And(Expression.Sql(" Password = ? COLLATE Latin1_General_CS_AS" , password, NHibernateUtil.String)); .Future() ; 

In the above statement, Latin1_General_CS_AS will be used, where CS means: case sensitive and AS code. Emphasis sensitive

In addition, there is some project of the custom expression LikeExpression, which can consume the COLLATE string as a constant or from an installation:

+2
source

Another approach, not QueryOver, but LINQ:

 session.Query<User>().Where(u => SqlMethods.Like(u.Username, "something")).ToList(); 
-1
source

Or, with the criteria:

 session.CreateCriteria(typeof(User), "u").Add(Restrictions.Like(Projections.Property("u.Username"), "something")).List<Username>(); 
-1
source

Finally, QueryOver:

 session.QueryOver<User>().Where(Expression.Sql("Username LIKE ?", "something", NHibernateUtil.String)).List<User>() 
-1
source

Source: https://habr.com/ru/post/892908/


All Articles