ベースには多対多のタイプがあります.. 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>