문제

After several hours of reading through several similar issues, I am still unable to find the issue I'm having. The exception is occurring in the query which is as follows:

    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

The exception is "Invalid Cast (check your mapping for property type mismatches); setter of TestProject.TestCase"

With an InnerException of {"Specified cast is not valid."}

The class for this is as follows:

    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; }

And the mapping file is

    <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>

I'm not overly familiar with SQL but it has helped me in debugging this project in the past, so I'll post what's generated:

    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_

I have been able to load values into the table using this mapping, but I have been unable to use queries of any kind without finding this error. I've looked myself and I cannot seem to find the cause.

What I've tried:
-Different queries
-Changing DateTime? to DateTime (read this as a possible solution somewhere)
-Played around with the structure of the line where the exception occurred

Any help with this would be greatly appreciated as I've been stuck on this and similar basic issues for several hours over the past few days.

도움이 되었습니까?

해결책

Found a solution, and I'm not sure why I didn't think about it before.

By removing the types from the .hbm.xml, it let the queries go through unhindered. After some more testing, I have concluded that the Priority column was not happy about being an int OR a uint. I assume this is an issue with how MySQL stores the integers in comparison to how C# does, most likely because I have them flagged as unsigned on the database side. As a fix, I have simply removed that type from the XML.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top