3

基础类型为多对多 .. 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

BDDoctor.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>();
     }

 } }

BP患者.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; }


 } }

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

BP患者.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);
     }

应用程序配置

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

1 回答 1

2

这里的铸造问题是由以下事实引起的,即有两个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>and Iesi ISet<>,或者您可以将映射更改为<bag>并使用本机 c# IList<>。但是在您的集合映射中,您必须为多对多关系设置正确的column,而不是

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

如果是 BNote 表上的 ID 列,最好使用<idbag>

于 2013-10-23T03:37:28.327 回答