FluentNHibernate - Unable to perform find[SQL: SQL not available]

1,410 views
Skip to first unread message

Praveen Liyanaarachchi

unread,
May 9, 2016, 6:56:07 AM5/9/16
to nhusers
Hello,
I just trying to access data using FluentNhibernate / C# and i got following error message when i trying to execute a sql query. I would appreciate any help

Exception: NHibernate.Exceptions.GenericADOException: Unable to perform find[SQL: SQL not available] ---> System.ArgumentException: The value "SampleProject.User" is not of type "SampleProject.User" and cannot be used in this generic collection. Parameter name: value at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType) at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item) at NHibernate.Util.ArrayHelper.AddAll(IList to, IList from)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) --- End of inner exception stack trace --- at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) at NHibernate.Impl.CriteriaImpl.List(IList results) at NHibernate.Impl.CriteriaImpl.ListT


my scripts are looks like follows.Seems like something wrong with FluentMappings 

namespace SampleProject
{
public class NHibernateHelper
{
    public NHibernateHelper()
    {
        InitializeSessionFactory();
    }
    private static ISessionFactory m_SessionFactory;
    public static ISessionFactory SessionFactory
    {
        get
        {
            if (m_SessionFactory == null)
                InitializeSessionFactory();
            return m_SessionFactory;
        }
    }
    private static void InitializeSessionFactory()
    {
        try
        {
            string connectionString = "";
            connectionString = "Server=localhost;Port=3306;Database=myDB;Uid=testUser;Pwd=123; Allow Zero Datetime=true;Convert Zero Datetime=true; CharSet=utf8";
            m_SessionFactory = Fluently.Configure().Database(
            MySQLConfiguration.Standard
            .ConnectionString(connectionString))
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<GetData>())
            .ExposeConfiguration(cfg => cfg.SetProperty("connection.release_mode", "on_close"))
            .BuildSessionFactory();
        }
        catch (FluentConfigurationException e)
        {
            throw e.InnerException;
        }
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }

}

}


public class User
{
    public virtual int UserID { get; set; }
    public virtual string Name { get; set; }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.UserID ).Column("user_id");
        Map(x => x.Name).Column("name");
        Table("User_master");
     }
}

// Execute sql query

using (var session = NHibernateHelper.OpenSession())
{
      using (var transaction = session.BeginTransaction())
      {
          var userM = session.CreateCriteria<User>("usr").List<User>();


      }
} 


Paulo Quicoli

unread,
May 9, 2016, 7:05:50 AM5/9/16
to nhu...@googlegroups.com
Hi,

var userM = session.CreateCriteria<User>("usr").List<User>();
I think you don't need this "usr"  in  CreatCriteria because you are not using projections... (see: http://nhibernate.info/doc/nhibernate-reference/querycriteria.html)

"The Alias() and As() methods simply wrap a projection instance in another, aliased, instance of IProjection."




--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Praveen Liyanaarachchi

unread,
May 10, 2016, 6:28:27 AM5/10/16
to nhusers
Hi Quicoli,
Thanks for the reply. Yes, i agreed. no need to use "usr" here, but without it also i'm getting the same error 

Paulo Quicoli

unread,
May 10, 2016, 6:40:04 AM5/10/16
to nhu...@googlegroups.com
Hi,

I'm not an expert in MySQL but, maybe "user" wouldn't be a reserved word from the database? I believe you would have to use

.ExposeConfiguration(cfg => SchemaMetadataUpdater.QuoteTableAndColumns);




Praveen Liyanaarachchi

unread,
May 12, 2016, 6:11:02 AM5/12/16
to nhusers
I already tried with another table, other-than "user" . so without "user" keyword also it gives me the same error :(. Thanks again for the reply


On Monday, May 9, 2016 at 7:56:07 PM UTC+9, Praveen Liyanaarachchi wrote:

Paulo Quicoli

unread,
May 12, 2016, 6:21:07 AM5/12/16
to nhu...@googlegroups.com
would you mind to send me a simple project showing that error? A simple console application would be enough.

thanks

Praveen Liyanaarachchi

unread,
May 13, 2016, 9:17:45 AM5/13/16
to nhusers
Thank you very much. I have created sample project and also included a readme file how to setup the environment, hope my readme file is clear enough . 
Please download the sample project from here


On Monday, May 9, 2016 at 7:56:07 PM UTC+9, Praveen Liyanaarachchi wrote:

Paulo Quicoli

unread,
May 13, 2016, 9:56:20 AM5/13/16
to nhu...@googlegroups.com
downloading right now....  ASAP I get a clue, I tell you.


bye

Praveen Liyanaarachchi

unread,
May 19, 2016, 10:27:43 AM5/19/16
to nhusers
Thank you very much. please let me know if you having any problems to setting up the project. 


On Monday, May 9, 2016 at 7:56:07 PM UTC+9, Praveen Liyanaarachchi wrote:

Paulo Quicoli

unread,
May 20, 2016, 1:27:13 PM5/20/16
to nhu...@googlegroups.com
I'm sorry for taking so long... I was in a trip. 

I'm looking it tonight



Reply all
Reply to author
Forward
0 new messages