'FluentNHibernate - Unable to perform find[SQL: SQL not available]
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>();
}
}
Solution 1:[1]
I know this is a very old question but when Was looking for a solution to the same issue I did not see any helpful answers here. So, I thought to put my comments here
I was having the same issue and I resolved it by correcting the right property type
in your case, the error is telling that the issue is not both user types are same even though it looks the same. you might have 2 user types with a similar name but could be in different classes.
The value "SampleProject.User" is not of type "SampleProject.User" and cannot be used in this generic collection.
So please go to the all NHField<> properties of the expected USER class and see what type of property return values are expected for each property and what type of your returning properties? if one the property is not returning as expected type the error shows for the whole User class type.
Let me know if you need further assistance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Softtech |
