user2908367
user2908367

Reputation: 33

Invalid Cast when the record is guid

The base has a type of many-to-many .. ID is of type GUID ... while keeping out this error:

{"Unable to cast object type \"NHibernate.Collection.Generic.PersistentGenericSet`1[Hospital1.Domain.BPatient]\" the type \"System.Collections.Generic.ISet`1[Hospital1.Domain.BPatient]\"."}

Message: 
Invalid Cast (check your mapping for property type mismatches); setter of Hospital1.Domain.BDoctor

BDoctor.cs

 namespace Hospital1.Domain {
 public class BDoctor
 {
     public virtual Guid id { get; set; }
     public virtual string doctor { get; set; }
     public virtual ISet<BPatient> BPatients { get; set; }


     public BDoctor()
     {
         BPatients = new HashSet<BPatient>();
     }

 } }

BPatient.cs

 namespace Hospital1.Domain {
 public class BPatient
 {
     public virtual Guid id { get; set; }
     public virtual string name { get; set; }
     public virtual int growth { get; set; }
     public virtual int weight { get; set; }
     public virtual ISet<BDoctor> BDoctors { get; set; }


 } }

BDoctor.hbm.xml

 <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping
                xmlns="urn:nhibernate-mapping-2.2"
                assembly="Hospital1"
                namespace="Hospital1.Domain">   <class name="BDoctor" table="BDoctor">

  <id name="id" column="id">
   <generator class="guid.comb" />
 </id>


 <property name="doctor" column="doctor"/>
 <set name="BPatients" table="BNote">

   <key column="iddoc" />
   <many-to-many class="BPatient" column="id" />

 </set>   </class> </hibernate-mapping>

BPatient.hbm.xml

 <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping
                xmlns="urn:nhibernate-mapping-2.2"
                assembly="Hospital1"
                namespace="Hospital1.Domain">   <class name="BPatient" table="BPatient">


 <id name="id" column="id"><generator class="guid.comb" />
 </id>


 <property name="name" column="name"/>
 <property name="growth" column="growth"/>
 <property name="weight" column="weight"/>



 <set name="BDoctors" table="BNote" inverse="true">
   <key column="idpat" />
   <many-to-many class="BDoctor" column="id" />

 </set>   </class> </hibernate-mapping>

XAML.CS

         myConfiguration = new Configuration();
         myConfiguration.Configure();
         mySessionFactory = myConfiguration.BuildSessionFactory();
         mySession = mySessionFactory.OpenSession();


         BPatient patient = new BPatient();
         patient.name = "Roman";
         patient.growth = Convert.ToInt32("183");
         patient.weight = Convert.ToInt32("90");
         mySession.Save(patient);

         BDoctor doctori = new BDoctor();
         doctori.doctor = "Andry";
         doctori.BPatients.Add(patient);
         mySession.Save(doctori);
     }

App.config

 <?xml version="1.0" encoding="utf-8" ?> <configuration>  
 <configSections>
     <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />  
 </configSections>

   <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
     <session-factory>
       <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
       <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
       <property name="query.substitutions">hqlFunction=SQLFUNC</property>
       <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
       <property name="connection.connection_string">Data Source=(local);Initial Catalog=Hospital;Integrated
 Security=True</property>
       <property name="show_sql">true</property>
       <mapping assembly="Hospital1" />
     </session-factory>   </hibernate-configuration>
      <startup> 
         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
     </startup> </configuration>

Upvotes: 3

Views: 399

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

The casting issue here is caused by the fact, that there are two ISet, and you are referencing the "wrong" one.

I mean, in your code, you'd have (well, I would say):

using System.Collections.Generic; // where ISet is
...
public class BDoctor
{
     public virtual ISet<BPatient> BPatients { get; set; } // System... ISet

While NHibernate works with Iesi.Collections.1.0.1 collections

using Iesi.Collections.Generic;  // ISet<>

You can get this package via nuget:

<package targetFramework="net40" version="1.0.1.0"    id="Iesi.Collections" />

EDIT: After we've solved Iesi... let's fix next

So, you can use <set> and Iesi ISet<>, or you can change mapping to <bag> and use native c# IList<>. But in your collection mapping, you have to set the correct column, for the many-to-many relation, so instead of

<set name="BDoctors" table="BNote" inverse="true">
   <key column="idpat" />
   <many-to-many class="BDoctor" column="id" />
</set> 
..
<set name="BPatients" table="BNote">
   <key column="iddoc" />
   <many-to-many class="BPatient" column="id" />
</set>

For IList<> we can use bag, and see the difference in the column mapping

<bag name="BDoctors" table="BNote" inverse="true">
   <key column="idpat" />
   <many-to-many class="BDoctor" column="iddoc" />
</bag> 
..
<bag name="BPatients" table="BNote">
   <key column="iddoc" />
   <many-to-many class="BPatient" column="idpat" />
</bag>

In case, that htere is ID column on the BNote table, even better would be to use <idbag>

Upvotes: 2

Related Questions