经过几个小时的阅读几个类似的问题,我仍然找不到我的问题。异常发生在查询中,如下所示:
Configuration config = new Configuration();
config.AddAssembly(typeof(TestCase).Assembly);
ISessionFactory sessionFactory = config.BuildSessionFactory();
var session = sessionFactory.OpenSession();
ICriteria targetObjects = session.CreateCriteria(typeof(TestCase));
IList<TestCase> itemList = targetObjects.List<TestCase>(); //Exception raised here异常是“无效的强制转换(检查属性类型不匹配的映射);TestProject.TestCase的setter”
如果InnerException为{“指定的强制转换无效。”}
这方面的课程如下:
public virtual Guid Id { get; set; }
public virtual ushort Priority { get; set; }
public virtual string Description { get; set; }
public virtual IList<string> Project { get; set; }
public virtual string Name { get; set; }
public virtual DateTime? Created { get; set; }
public virtual DateTime? Updated { get; set; }映射文件是
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="TestProject"
namespace="TestProject">
<class name="TestCase"
table="test_table"
lazy="true">
<id name="Id" column="ID" type="Guid">
<generator class="assigned" />
</id>
<property name="Priority" column="Priority"
type="int" />
<property name="Description" column="Description"
type="string" />
<property name="Name" column="Name"
type="string" />
<property name="Created" column="Created"
type="DateTime" />
<property name="Updated" column="Updated"
type="DateTime" />
<list table="ProjTable" name="Project">
<key column="TestID" />
<list-index column="ProjIndex" />
<element column="ProjCol" />
</list>
</class>
</nhibernate-mapping>我不太熟悉SQL,但它在过去帮助我调试了这个项目,所以我将发布生成的内容:
NHibernate: SELECT this_.ID as ID0_0_, this_.Priority as Priority0_0_,
this_.Description as Descript4_0_0_, this_.Test_Name as Test5_0_0_,
this_.Created as Created0_0_, this_.Updated as Updated0_0_ FROM test_table this_我已经能够使用此映射将值加载到表中,但在没有找到此错误的情况下,我无法使用任何类型的查询。我看了看自己,似乎找不到原因。
我试过的是:
-Different查询
-Changing DateTime?到DateTime (将其理解为可能的解决方案)
-Played周围有发生异常的行的结构。
对此的任何帮助都将是非常感谢的,因为在过去的几天里,我已经在这个问题和类似的基本问题上被困了好几个小时。
发布于 2014-03-17 16:32:25
找到了解决办法,我不知道为什么我以前没想过。
通过从.hbm.xml中删除类型,可以使查询不受阻碍地进行。经过更多的测试后,我得出结论,优先级列对于成为int或uint并不满意。我认为这是MySQL如何存储整数的问题,与C#的存储方式相比,这很可能是因为我在数据库端将它们标记为无符号。作为一个修复,我只是从XML中删除了该类型。
https://stackoverflow.com/questions/22380065
复制相似问题