保存NHibernate的图像列表

时间:2015-08-27 15:23:20

标签: c# nhibernate

我想用NHibernate存储一个图像列表,请帮帮我。

Dossier.class

public class Dossier
    {
            public virtual int ID { get; set; }
            public virtual string Compagnie_assurance { get; set; }   
            public virtual string Agent_assurance { get; set; } 
            public virtual string Nom_assure { get; set; }
            public virtual string Prenom_assure { get; set; } 
            public virtual string Marque { get; set; }    
            public virtual string Matricule { get; set; }      
            public virtual string Telephone { get; set; }      
            public virtual DateTime Date_accident { get; set; }
            public virtual DateTime Date_mission { get; set; }
            public virtual DateTime Date_expertise { get; set; }
            public virtual string Carte_grise { get; set; }
            public virtual string PV { get; set; }
            public virtual string Decl { get; set; }
            public virtual string CA { get; set; }
            public virtual string Type_dossier  { get; set; }
            public virtual string moin20000 { get; set; }
            public virtual string plus20000 { get; set; }
            public virtual string visibilite_Syteme { get; set; }
            public virtual IList<Image> listImages { get; set; }
    }

Dossier.hbm.xml

 <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="ExpertiseSHOUL"
                       namespace="ExpertiseSHOUL">

      <class name="Dossier">
        <id name="ID" >
          <generator class="identity" />
        </id>
        <property name="Compagnie_assurance" />
        <property name="Agent_assurance" />
        <property name="Nom_assure" />
        <property name="Prenom_assure" />
        <property name="Marque" />
        <property name="Matricule" />
        <property name="Telephone" />
        <property name="Date_accident" />
        <property name="Date_mission" />
        <property name="Date_expertise" />
        <property name="Carte_grise" />
        <property name="PV" />
        <property name="Decl" />
        <property name="CA" />
        <property name="Type_dossier" />
        <property name="moin20000" />
        <property name="plus20000" />
        <property name="visibilite_Syteme" />
        <list name="listImages" cascade="all">
          <key column="DossierId"/>
          <index column="ImageIndex"/>
          <one-to-many class="Image"/>
        </list>

      </class>

    </hibernate-mapping>

Image.class

public class Image
    {
        public virtual int ID { get; set; }
        public virtual string url { get; set; }
        public virtual byte[] image { get; set; }
        public virtual Dossier dossier { get; set; }
    }

Image.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="ExpertiseSHOUL"
                   namespace="ExpertiseSHOUL">

  <class name="Image">
    <id name="ID" >
      <generator class="identity" />
    </id>
    <property name="url" />
    <property name="image" />
    <many-to-one name="dossier" column="DossierId" />

  </class>

</hibernate-mapping>

我保存的方法:

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





            using (mySession.BeginTransaction())
            {
                Dossier dossier = new Dossier();
                dossier.Compagnie_assurance = "Compagnie1";
                dossier.Agent_assurance = "Agent1";
                dossier.Date_accident = DateTime.Now;
                dossier.Date_expertise = DateTime.Now;
                dossier.Date_mission = DateTime.Now; 
                // i want code to insert here in order to save a list of images

                mySession.Save(dossier);
                mySession.Transaction.Commit();

            }

但我希望添加代码以便使用图像列表保存每个档案。 注意:代码工作正常!所以我想要juste代码来完成动作。 非常感谢!

1 个答案:

答案 0 :(得分:0)

我真的不知道你想要什么,但试试这个:

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

using (mySession.BeginTransaction())
{
    Dossier dossier = new Dossier();
    dossier.Compagnie_assurance = "Compagnie1";
    dossier.Agent_assurance = "Agent1";
    dossier.Date_accident = DateTime.Now;
    dossier.Date_expertise = DateTime.Now;
    dossier.Date_mission = DateTime.Now; 

    IList<Image> listImages = new IList<Image>();

    for (int i = 0; i <nbImage ; i++)
    {
        Image img = new Image();   
        img.ID = i;
        img.url = "";
        img.image = "";
        img.Dossier = dossier;
        listImages.Add(img);
    }      

    dossier.listImages = listImages; 

    mySession.Save(dossier);
    mySession.Transaction.Commit();
}