我已经用这个头挠了一段时间了,对出什么问题我一无所知。

概述:我在MySQL数据库中有两个表。两者都正确映射到数据库(我可以加载数据),并且能够查询一个表,但不能查询另一个表。

我研究过的解决方案:表和C#代码之间的类型转换问题,映射问题,SQL格式问题。

失败的代码如下:

Configuration config = new Configuration();
config.Configure();
ISessionFactory sessionFactory = config.BuildSessionFactory();
var schema = new SchemaUpdate(config);
schema.Execute(true, true);
results = session.CreateSQLQuery("SELECT * FROM Stats_Table") // Exception thrown here
    .AddEntity(typeof(TestStats))
    .List<TestStats>();




班级:

public class Stats
{
    public virtual Guid      Id        { get; set; }
    public virtual Guid      TestId    { get; set; }
    public virtual String    Name      { get; set; }
    public virtual TResult   Result    { get; set; }
    public virtual DateTime? Timestamp { get; set; }
    public virtual UInt32    Duration  { get; set; }
    public virtual String    Notes     { get; set; }

    public Stats()
    {
    }

    public Stats(TestCase tc, TResult Res, DateTime? Time, UInt32 Dura, String ResultNote="")
    {
        Id = Guid.NewGuid();
        TestId = tc.Id;
        Name = tc.TestName;
        Result = Res;
        Timestamp = Time;
        Duration = Dura;
        Notes = ResultNote;
    }

    public Stats(Guid T_Id, string Name, TResult Res, DateTime? Time, UInt32 Dura, String ResultNote="")
    {
        Id = Guid.NewGuid();
        TestId = T_Id;
        Name = Name;
        Result = Res;
        Timestamp = Time;
        Duration = Dura;
        Notes = ResultNote;
    }
}




映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Test_Database"
                   namespace="Test_Database.TestClasses">
  <class name="Test_Database.TestClasses.Stats"
         table="Stats_Table"
         lazy="true">

    <id name="Id" column="ID" type="Guid">
      <generator class="assigned" />
    </id>

    <property name="TestId"    column="TestID" />
    <property name="Name"      column="Name" />
    <property name="Result"    column="Result" />
    <property name="Timestamp" column="Timestamp" />
    <property name="Duration"  column="Duration" />
    <property name="Notes"     column="Notes" />

  </class>
</hibernate-mapping>




我收到NHibernate GenericADOException:“无法执行查询\ r \ n [SELECT * FROM Stats_Table] \ r \ n [SQL:SELECT * FROM Stats_Table]”

带有InnerException:“输入字符串的格式不正确。”



堆栈跟踪:

在System.Number.StringToNumber处(字符串str,NumberStyles选项,NumberBuffer&数字,NumberFormatInfo信息,布尔值parseDecimal)
   在System.Number.ParseInt32(String s,NumberStyles样式,NumberFormatInfo信息)
   在System.String.System.IConvertible.ToInt32(IFormatProvider提供程序)
   在System.Convert.ToInt32(对象值)
   在NHibernate.Type.PersistentEnumType.SystemInt32EnumConverter.Convert(对象输入)在p:\ nhibernate-core \ src \ NHibernate \ Type \ PersistentEnumType.cs:line 88
   在NHibernate.Type.PersistentEnumType.AbstractEnumConverter`1.ToObject(Type enumClass,Object code)在p:\ nhibernate-core \ src \ NHibernate \ Type \ PersistentEnumType.cs:line 33
   在NHibernate.Type.PersistentEnumType.GetInstance(对象代码)在p:\ nhibernate-core \ src \ NHibernate \ Type \ PersistentEnumType.cs:line 203
   在NHibernate.Type.PersistentEnumType.Get(IDataReader rs,Int32索引)中的p:\ nhibernate-core \ src \ NHibernate \ Type \ PersistentEnumType.cs:line 189
   在NHibernate.Type.NullableType.NullSafeGet(IDataReader rs,字符串名称)在p:\ nhibernate-core \ src \ NHibernate \ Type \ NullableType.cs:line 253
   在NHibernate.Type.NullableType.NullSafeGet(IDataReader rs,String []名称,ISessionImplementor会话,对象所有者)在p:\ nhibernate-core \ src \ NHibernate \ Type \ NullableType.cs:line 195
   在NHibernate.Type.AbstractType.Hydrate(IDataReader rs,String []名称,ISessionImplementor会话,对象所有者)在p:\ nhibernate-core \ src \ NHibernate \ Type \ AbstractType.cs:line 131
   在NHibernate.Persister.Entity.AbstractEntityPersister.Hydrate(IDataReader rs,Object id,Object obj,ILoadable rootLoadable,String [] [] suffixedPropertyColumns,Boolean allProperties,ISessionImplementor session)在p:\ nhibernate-core \ src \ NHibernate \ Persister Entity \ AbstractEntityPersister.cs:第2518行
   在NHibernate.Loader.Loader.LoadFromResultSet(IDataReader rs,Int32 i,Object obj,String instanceClass,EntityKey key,String rowIdAlias,LockMode lockMode,ILoadable rootPersister,ISessionImplementor session)中位于p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第989行
   在NHibernate.Loader.Loader.InstanceNotYetLoaded(IDataReader dr,Int32 i,ILoadable持久性,EntityKey键,LockMode lockMode,字符串rowIdAlias,EntityKey optionalObjectKey,Object optionalObject,IList hydratedObjects,ISessionImplementor会话)中\ Loader \ Loader.cs:第944行
   在NHibernate.Loader.Loader.GetRow(IDataReader rs,ILoadable []持久性,EntityKey []键,Object optionalObject,EntityKey optionalObjectKey,LockMode [] lockModes,IList hydratedObjects,ISessionImplementor会话中)在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第876行
   在NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet,ISessionImplementor session,QueryParameters queryParameters,LockMode [] lockModeArray,EntityKey optionalObjectKey,IList hydredObjects,EntityKey [] keys,Boolean returnProxies)在p:\ nhibernate-core \ ser \ NHibernate \ Loader.cs:第342行
   在NHibernate.Loader.Loader.DoQuery(ISessionImplementor会话,QueryParameters queryParameters,Boolean returnProxies)在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:line 473
   在NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor会话,QueryParameters queryParameters,Boolean returnProxies)中的p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:line 251中
   在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:line 1564中的NHibernate.Loader.Loader.DoList(ISessionImplementor会话,QueryParameters queryParameters)



从NHibernate:

在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:line 1573中的NHibernate.Loader.Loader.DoList(ISessionImplementor会话,QueryParameters queryParameters)
   在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:line 1472中的NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session,QueryParameters queryParameters)
   在NHibernate.Loader.Loader.List(ISessionImplementor会话,QueryParameters queryParameters,ISet`1 querySpaces,IType [] resultTypes)在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:line 1467中
   在NHibernate.Loader.Custom.CustomLoader.List(ISessionImplementor会话,QueryParameters queryParameters)在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Custom \ CustomLoader.cs:line 276中
   在NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery,QueryParameters queryParameters,IList结果)在p:\ nhibernate-core \ src \ NHibernate \ Impl \ SessionImpl.cs:line 2108
   在NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec,QueryParameters queryParameters,IList结果)在p:\ nhibernate-core \ src \ NHibernate \ Impl \ SessionImpl.cs:line 2091
   在NHibernate.Impl.SessionImpl.List [T](NativeSQLQuerySpecification spec,QueryParameters queryParameters)在p:\ nhibernate-core \ src \ NHibernate \ Impl \ SessionImpl.cs:line 2077
   在NHibernate.Impl.SqlQueryImpl.ListT在p:\ nhibernate-core \ src \ NHibernate \ Impl \ SqlQueryImpl.cs:line 163
   在C:\ Users \ cryan \ Documents \ Visual Studio 2010 \ Projects \ TestManager_Database \ TestManager_Database \ Program.cs:line的TestManager_Database.Program.Main(String [] args)中
   在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String []参数)
   在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)中
   在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
   在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
   在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态)
   在System.Threading.ThreadHelper.ThreadStart()

我已经尝试过NHibernate的源代码,但是我从中没有发现任何问题。

任何帮助,将不胜感激。

最佳答案

您的枚举-TResult可能被保存为字符串值,并试图解析为int值(根据错误消息进行猜测)

尝试将您的result属性的配置更改为:

<property name="Result" column="Result" type="NHibernate.Type.EnumStringType`1[[MyNamespace.TResult, MyAssembly]], NHibernate"/>


其中MyNamespace.TResult,MyAssembly是您的枚举TResult的全限定名

或者,将结果列更改为int :)

关于c# - NHibernate-无法执行查询-输入字符串的格式不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22720845/

10-16 15:41