我只是尝试使用 FluentNhibernate / C#访问数据,当我尝试执行sql查询时,我收到以下错误消息。我将不胜感激任何帮助
异常:NHibernate.Exceptions.GenericADOException:无法执行 执行find [SQL:SQL不可用] ---> System.ArgumentException: 值" SampleProject.User"不属于" SampleProject.User"和 不能在此通用集合中使用。参数名称:值at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, 输入targetType) System.Collections.Generic.List`1.System.Collections.IList.Add(对象 item)at NHibernate.Util.ArrayHelper.AddAll(IList to,IList from)
在NHibernate.Impl.SessionImpl.List(CriteriaImpl标准,IList 结果)---内部异常堆栈跟踪结束--- at NHibernate.Impl.SessionImpl.List(CriteriaImpl条件,IList结果) 在NHibernate.Impl.CriteriaImpl.List(IList结果)at NHibernate.Impl.CriteriaImpl.ListT
我的脚本看起来如下。像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");
}
}
//执行sql查询
using (var session = NHibernateHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var userM = session.CreateCriteria<User>("usr").List<User>();
}
}