当记录为 GUID 时无效强制转换

本文关键字:转换 无效 记录 GUID | 更新日期: 2023-09-27 18:36:23

基有多对多的类型。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

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

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

应用配置

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

当记录为 GUID 时无效强制转换

这里的铸造问题是由以下事实引起的:有两个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>

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