簡體   English   中英

記錄為guid時無效的強制轉換

[英]Invalid Cast when the record is guid

基類有多對多的類型.ID類型為GUID ...同時不會出現此錯誤:

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

這里的轉換問題是由於有兩個ISet ,你引用了“錯誤”的問題。

我的意思是,在你的代碼中,你有(好吧,我會說)

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

而NHibernate與Iesi.Collections.1.0.1集合一起使用

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

你可以通過nuget獲得這個包:

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

編輯:在我們解決了Iesi之后 ...讓我們接下來解決

因此,您可以使用<set>和Iesi ISet<> ,或者您可以將映射更改為<bag>並使用本機c# IList<> 但是在您的集合映射中,您必須為多對多關系設置正確的 ,而不是

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

對於IList<>我們可以使用bag,並查看列映射的差異

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

如果那個htere是BNote表上的ID列,那么更好的方法是使用<idbag>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM