NHibernate此SqlParameterCollection的索引N无效,Count = N.

时间:2015-08-26 14:24:21

标签: c# xml nhibernate nhibernate-mapping

当我尝试在属性多对一中插入属性insert = true的新实体时出现问题。 一旦事务提交我就会收到错误(此SqlParameterCollection的索引N无效,Count = N) 与映射文件commandeclient.hbm.xml发生的情况 并且对于Adresselivraisonclient.hbm.xml,即使使用insert = true

也很好

问题是我在Adresselivraisonclient.hbm.xml上有复合键,这个表与commandeclient.hbm.xml有关,其中两个表都包含[client]列。

有没有改变名称的任何解决方案?

这里有2个地图文件。

commandeclient.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BddCmd.Data" assembly="BddCmd.Data" >
        <class name="Commandeclient" table="[dbo].[commandeclient]" >
            <id name="Piece" column="[piece]" type="Int32" >
                <generator class="assigned" />
            </id>
            <property name="Datepiece" column="[datepiece]" type="DateTime" not-null="true" /> 
            <property name="Datelivraison" column="[datelivraison]" type="DateTime" not-null="true" /> 
            <property name="Cloture" column="[cloture]" type="Boolean" not-null="false" /> 
            <property name="Nom" column="[nom]" type="String" not-null="false" length="50" /> 
            <property name="Adresse" column="[adresse]" type="String" not-null="false" length="35" /> 
            <property name="Complement" column="[complement]" type="String" not-null="false" length="150" /> 
            <property name="Codepostal" column="[codepostal]" type="String" not-null="false" length="10" /> 
            <property name="Ville" column="[ville]" type="String" not-null="false" length="35" /> 
            <property name="Commentaire" column="[commentaire]" type="String" not-null="false" length="16" /> 
            <property name="Utilisationtva" column="[utilisationtva]" type="Boolean" not-null="false" /> 
            <property name="Libelle" column="[libelle]" type="String" not-null="false" length="100" /> 
            <property name="Montantht" column="[montantht]" type="Double" not-null="false" /> 
            <property name="Tauxremise" column="[tauxremise]" type="Double" not-null="false" /> 
            <property name="Montantremise" column="[montantremise]" type="Double" not-null="false" /> 
            <property name="Tauxescompte" column="[tauxescompte]" type="Double" not-null="false" /> 
            <property name="Montantescompte" column="[montantescompte]" type="Double" not-null="false" /> 
            <property name="Montantnetht" column="[montantnetht]" type="Double" not-null="false" /> 
            <property name="Montantdivers1" column="[montantdivers1]" type="Double" not-null="false" /> 
            <property name="Cptcomptabledivers1" column="[cptcomptabledivers1]" type="String" not-null="false" length="20" /> 
            <property name="Montantdivers2" column="[montantdivers2]" type="Double" not-null="false" /> 
            <property name="Cptcomptabledivers2" column="[cptcomptabledivers2]" type="String" not-null="false" length="20" /> 
            <property name="Portht" column="[portht]" type="Double" not-null="false" /> 
            <property name="Compteport" column="[compteport]" type="String" not-null="false" length="20" /> 
            <property name="Codetvaport" column="[codetvaport]" type="Int32" not-null="false" /> 
            <property name="Tauxtvaport" column="[tauxtvaport]" type="Double" not-null="false" /> 
            <property name="Montanttvaport" column="[montanttvaport]" type="Double" not-null="false" /> 
            <property name="Comptetvaport" column="[comptetvaport]" type="String" not-null="false" length="20" /> 
            <property name="Totalht" column="[totalht]" type="Double" not-null="false" /> 
            <property name="Montanttva" column="[montanttva]" type="Double" not-null="false" /> 
            <property name="Montantttc" column="[montantttc]" type="Double" not-null="false" /> 
            <property name="Acompte" column="[acompte]" type="Double" not-null="false" /> 
            <property name="Totalnet" column="[totalnet]" type="Double" not-null="false" /> 
            <property name="Pieceedite" column="[pieceedite]" type="Boolean" not-null="false" /> 
            <property name="Refcdeclient" column="[refcdeclient]" type="String" not-null="false" length="40" /> 
            <property name="Compteclient" column="[compteclient]" type="String" not-null="false" length="20" /> 
            <property name="Designationdevise" column="[designationdevise]" type="String" not-null="false" length="70" /> 
            <property name="Abreviationdevise" column="[abreviationdevise]" type="String" not-null="false" length="6" /> 
            <property name="Tauxdevise" column="[tauxdevise]" type="Double" not-null="false" /> 
            <property name="Suivipar" column="[suivipar]" type="String" not-null="false" length="50" /> 
            <property name="Changemontantremise" column="[changemontantremise]" type="Boolean" not-null="false" /> 
            <property name="Changemontantescompte" column="[changemontantescompte]" type="Boolean" not-null="false" /> 
            <property name="Releve" column="[releve]" type="Boolean" not-null="false" /> 
            <property name="Montantfodec" column="[montantfodec]" type="Double" not-null="false" /> 
            <property name="Montantimccv" column="[montantimccv]" type="Double" not-null="false" /> 
            <property name="Tauxfodec" column="[tauxfodec]" type="Double" not-null="false" /> 
            <property name="Tauximccv" column="[tauximccv]" type="Double" not-null="false" /> 
            <property name="Transporteur" column="[transporteur]" type="String" not-null="false" length="25" /> 
            <property name="Poidstotal" column="[poidstotal]" type="Double" not-null="false" /> 
            <property name="Volumetotal" column="[volumetotal]" type="Double" not-null="false" /> 
            <property name="Codetvamontantdivers1" column="[codetvamontantdivers1]" type="Int32" not-null="false" /> 
            <property name="Tauxtvamontantdivers1" column="[tauxtvamontantdivers1]" type="Double" not-null="false" /> 
            <property name="Montanttvadivers1" column="[montanttvadivers1]" type="Double" not-null="false" /> 
            <property name="Codetvamontantdivers2" column="[codetvamontantdivers2]" type="Int32" not-null="false" /> 
            <property name="Tauxtvamontantdivers2" column="[tauxtvamontantdivers2]" type="Double" not-null="false" /> 
            <property name="Montanttvadivers2" column="[montanttvadivers2]" type="Double" not-null="false" /> 
            <property name="Comptetvamontantdivers1" column="[comptetvamontantdivers1]" type="String" not-null="false" length="20" /> 
            <property name="Comptetvamontantdivers2" column="[comptetvamontantdivers2]" type="String" not-null="false" length="20" /> 
            <property name="Porttransporteur" column="[porttransporteur]" type="Double" not-null="false" /> 
            <property name="Montantttceuro" column="[montantttceuro]" type="Double" not-null="false" /> 
            <property name="Langage" column="[langage]" type="String" not-null="false" length="250" /> 
            <property name="Tauxeuro" column="[tauxeuro]" type="Double" not-null="false" /> 
            <property name="Basetva0" column="[basetva0]" type="Double" not-null="false" /> 
            <property name="Basetva1" column="[basetva1]" type="Double" not-null="false" /> 
            <property name="Basetva2" column="[basetva2]" type="Double" not-null="false" /> 
            <property name="Basetva3" column="[basetva3]" type="Double" not-null="false" /> 
            <property name="Basetva4" column="[basetva4]" type="Double" not-null="false" /> 
            <property name="Basetva5" column="[basetva5]" type="Double" not-null="false" /> 
            <property name="Basetva6" column="[basetva6]" type="Double" not-null="false" /> 
            <property name="Montanttva1" column="[montanttva1]" type="Double" not-null="false" /> 
            <property name="Montanttva2" column="[montanttva2]" type="Double" not-null="false" /> 
            <property name="Montanttva3" column="[montanttva3]" type="Double" not-null="false" /> 
            <property name="Montanttva4" column="[montanttva4]" type="Double" not-null="false" /> 
            <property name="Montanttva5" column="[montanttva5]" type="Double" not-null="false" /> 
            <property name="Montanttva6" column="[montanttva6]" type="Double" not-null="false" /> 
            <property name="Montantremise0" column="[montantremise0]" type="Double" not-null="false" /> 
            <property name="Montantremise1" column="[montantremise1]" type="Double" not-null="false" /> 
            <property name="Montantremise2" column="[montantremise2]" type="Double" not-null="false" /> 
            <property name="Montantremise3" column="[montantremise3]" type="Double" not-null="false" /> 
            <property name="Montantremise4" column="[montantremise4]" type="Double" not-null="false" /> 
            <property name="Montantremise5" column="[montantremise5]" type="Double" not-null="false" /> 
            <property name="Montantremise6" column="[montantremise6]" type="Double" not-null="false" /> 
            <property name="Montantescompte0" column="[montantescompte0]" type="Double" not-null="false" /> 
            <property name="Montantescompte1" column="[montantescompte1]" type="Double" not-null="false" /> 
            <property name="Montantescompte2" column="[montantescompte2]" type="Double" not-null="false" /> 
            <property name="Montantescompte3" column="[montantescompte3]" type="Double" not-null="false" /> 
            <property name="Montantescompte4" column="[montantescompte4]" type="Double" not-null="false" /> 
            <property name="Montantescompte5" column="[montantescompte5]" type="Double" not-null="false" /> 
            <property name="Montantescompte6" column="[montantescompte6]" type="Double" not-null="false" /> 
            <property name="Codeclientfacture" column="[codeclientfacture]" type="String" not-null="false" length="25" /> 
            <property name="Situationdocument" column="[situationdocument]" type="Int32" not-null="false" /> 
            <property name="Dateexpedition" column="[dateexpedition]" type="DateTime" not-null="false" /> 
            <property name="Dateexpeditiondemandee" column="[dateexpeditiondemandee]" type="DateTime" not-null="false" /> 
            <property name="Plagelivraison" column="[plagelivraison]" type="String" not-null="false" length="255" /> 
            <property name="Plageexpedition" column="[plageexpedition]" type="String" not-null="false" length="255" /> 
            <property name="Codeclientpayeur" column="[codeclientpayeur]" type="String" not-null="false" length="25" /> 
            <many-to-one name="Devise" class="Devise" not-null="false" insert="true" update="true" >
                <column name="[devise]" />
            </many-to-one>
            <many-to-one name="Representant" class="Representant" not-null="false" insert="true" update="true" >
                <column name="[representant]" />
            </many-to-one>
            <many-to-one name="Modetaxation" class="Modetaxation" not-null="false" insert="true" update="true" >
                <column name="[modetaxation]" />
            </many-to-one>
            <many-to-one name="Dossier" class="Dossier" not-null="false" insert="true" update="true" >
                <column name="[dossier]" />
            </many-to-one>
            <many-to-one name="Region" class="Region" not-null="false" insert="true" update="true" >
                <column name="[region]" />
            </many-to-one>
            <many-to-one name="Client" class="Client" not-null="false" insert="true" update="true" >
                <column name="[client]" />
            </many-to-one>
            <many-to-one name="Modereglement" class="Modereglement" not-null="false" insert="true" update="true" >
                <column name="[modereglement]" />
            </many-to-one>
            <many-to-one name="Adresselivraisonclient" class="Adresselivraisonclient" not-null="false" insert="true" update="true" >
                <column name="[client]" />
                <column name="[adrlivraison]" />
            </many-to-one>
            <many-to-one name="Pay" class="Pay" not-null="false" insert="true" update="true" >
                <column name="[pays]" />
            </many-to-one>
            <many-to-one name="Departement" class="Departement" not-null="false" insert="true" update="true" >
                <column name="[departement]" />
            </many-to-one>
            <many-to-one name="Depot" class="Depot" not-null="false" insert="true" update="true" >
                <column name="[depot]" />
            </many-to-one>
            <many-to-one name="Civilite" class="Civilite" not-null="false" insert="true" update="true" >
                <column name="[civilite]" />
            </many-to-one>
            <many-to-one name="Interlocuteurclient" class="Interlocuteurclient" not-null="false" insert="true" update="true" >
                <column name="[correspondant]" />
            </many-to-one>
            <many-to-one name="Modeexpedition" class="Modeexpedition" not-null="false" insert="true" update="true" >
                <column name="[modeexpedition]" />
            </many-to-one>
            <many-to-one name="Modelivraison" class="Modelivraison" not-null="false" insert="true" update="true" >
                <column name="[modelivraison]" />
            </many-to-one>
            <many-to-one name="Port" class="Port" not-null="false" insert="true" update="true" >
                <column name="[paiementport]" />
            </many-to-one>
            <many-to-one name="Tarif" class="Tarif" not-null="false" insert="true" update="true" >
                <column name="[tarif]" />
            </many-to-one>
            <bag name="LignecommandeclientList" inverse="true" cascade="all-delete-orphan" lazy="true" >
                <key>
                    <column name="[piece]" />
                </key>
                <one-to-many class="Lignecommandeclient" />
            </bag>
            <bag name="EnteteofList" inverse="true" cascade="all-delete-orphan" lazy="true" >
                <key>
                    <column name="[codecommande]" />
                </key>
                <one-to-many class="Enteteof" />
            </bag>
        </class>
    </hibernate-mapping>

和Adresselivraisonclient.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BddCmd.Data" assembly="BddCmd.Data" >
        <class name="Adresselivraisonclient" table="[dbo].[adresselivraisonclient]" >
            <composite-id>
                <key-property name="Intitule" column="[intitule]" type="String" length="50" /> 
                <key-many-to-one name="Client" class="Client" >
                    <column name="[client]" />
                </key-many-to-one>
            </composite-id>
            <property name="Adresse" column="[adresse]" type="String" not-null="false" length="35" /> 
            <property name="Complement" column="[complement]" type="String" not-null="false" length="150" /> 
            <property name="Codepostal" column="[codepostal]" type="String" not-null="false" length="10" /> 
            <property name="Ville" column="[ville]" type="String" not-null="false" length="35" /> 
            <property name="Telephone" column="[telephone]" type="String" not-null="false" length="20" /> 
            <property name="Telecopie" column="[telecopie]" type="String" not-null="false" length="20" /> 
            <property name="Email" column="[email]" type="String" not-null="false" length="255" /> 
            <property name="Minitel" column="[minitel]" type="String" not-null="false" length="20" /> 
            <property name="Telecom1" column="[telecom1]" type="String" not-null="false" length="20" /> 
            <property name="Telecom2" column="[telecom2]" type="String" not-null="false" length="20" /> 
            <property name="Telecom3" column="[telecom3]" type="String" not-null="false" length="20" /> 
            <property name="Telecom4" column="[telecom4]" type="String" not-null="false" length="20" /> 
            <property name="Telecom5" column="[telecom5]" type="String" not-null="false" length="20" /> 
            <property name="Telecom6" column="[telecom6]" type="String" not-null="false" length="20" /> 
            <property name="Langage" column="[langage]" type="String" not-null="false" length="250" /> 
            <many-to-one name="Pay" class="Pay" not-null="false" insert="true" update="true" >
                <column name="[pays]" />
            </many-to-one>
            <many-to-one name="Departement" class="Departement" not-null="false" insert="true" update="true" >
                <column name="[departement]" />
            </many-to-one>
            <many-to-one name="Region" class="Region" not-null="false" insert="true" update="true" >
                <column name="[region]" />
            </many-to-one>
            <bag name="CdeouverteclientList" inverse="true" cascade="all-delete-orphan" lazy="true" >
                <key>
                    <column name="[client]" />
                    <column name="[adrlivraison]" />
                </key>
                <one-to-many class="Cdeouverteclient" />
            </bag>
            <bag name="BonlivraisonList" inverse="true" cascade="all-delete-orphan" lazy="true" >
                <key>
                    <column name="[client]" />
                    <column name="[adrlivraison]" />
                </key>
                <one-to-many class="Bonlivraison" />
            </bag>
            <bag name="CommandeclientList" inverse="true" cascade="all-delete-orphan" lazy="true" >
                <key>
                    <column name="[client]" />
                    <column name="[adrlivraison]" />
                </key>
                <one-to-many class="Commandeclient" />
            </bag>
            <bag name="DevisclientList" inverse="true" cascade="all-delete-orphan" lazy="true" >
                <key>
                    <column name="[client]" />
                    <column name="[adrlivraison]" />
                </key>
                <one-to-many class="Devisclient" />
            </bag>
        </class>
    </hibernate-mapping>

此处调用实体保存的代码

namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();


            }

            private void button1_Click(object sender, EventArgs e)
            {
                BddCmd.Data.BddDataContext dx = new BddCmd.Data.BddDataContext();
                BddCmd.Data.Commandeclient cmd = new BddCmd.Data.Commandeclient();

                cmd.Piece = (int)numericUpDown1.Value;

                cmd.Dossier = BddCmd.Data.DossierExtensions.GetByKey(dx.Dossier, 100);
                cmd.Datepiece = DateTime.Now;
                cmd.Datelivraison = DateTime.Now;
                cmd.Client = BddCmd.Data.ClientExtensions.GetByKey(dx.Client, "DECAT");
                cmd.Civilite = BddCmd.Data.CiviliteExtensions.GetByKey(dx.Civilite, "Mr");
                cmd.Dateexpedition = DateTime.Now;
                cmd.Dateexpeditiondemandee = DateTime.Now;
                cmd.Adresselivraisonclient = BddCmd.Data.AdresselivraisonclientExtensions.GetByKey(dx.Adresselivraisonclient, "Decathlon", "DECAT");
                cmd.Departement = BddCmd.Data.DepartementExtensions.GetByKey(dx.Departement, "01");
                cmd.Region = BddCmd.Data.RegionExtensions.GetByKey(dx.Region, "CENTRE");
                cmd.Pay = BddCmd.Data.PayExtensions.GetByKey(dx.Pay, 40);
                cmd.Depot = BddCmd.Data.DepotExtensions.GetByKey(dx.Depot, "01");
                cmd.Interlocuteurclient = BddCmd.Data.InterlocuteurclientExtensions.GetByKey(dx.Interlocuteurclient, 2);
                cmd.Port = BddCmd.Data.PortExtensions.GetByKey(dx.Port, 20);
                cmd.Devise = BddCmd.Data.DeviseExtensions.GetByKey(dx.Devise, 2);
                cmd.Modetaxation = BddCmd.Data.ModetaxationExtensions.GetByKey(dx.Modetaxation, "France");
                cmd.Modeexpedition = BddCmd.Data.ModeexpeditionExtensions.GetByKey(dx.Modeexpedition, 8);
                cmd.Modelivraison = BddCmd.Data.ModelivraisonExtensions.GetByKey(dx.Modelivraison, 5);
                cmd.Modereglement = BddCmd.Data.ModereglementExtensions.GetByKey(dx.Modereglement, "3 ECH");
                cmd.Representant = BddCmd.Data.RepresentantExtensions.GetByKey(dx.Representant, "AD");
                cmd.Tarif = BddCmd.Data.TarifExtensions.GetByKey(dx.Tarif, "Principal");


                Type t = cmd.GetType();
                System.Reflection.PropertyInfo[] props = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                foreach (var prop in props)
                {
                    if (prop.Name == "Piece")
                    {
                        continue;
                    }
                    if (prop.PropertyType == typeof(string))
                    {
                        prop.SetValue(cmd, "");
                    }
                    if(prop.PropertyType.IsPrimitive)
                    {
                        prop.SetValue(cmd, Activator.CreateInstance(prop.PropertyType));
                    }
                    else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        prop.SetValue(cmd, Activator.CreateInstance(Nullable.GetUnderlyingType(prop.PropertyType)));
                    }
                }

                try
                {
                    cmd.Save(dx);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }


                BddCmd.Data.Adresselivraisonclient alc = new BddCmd.Data.Adresselivraisonclient();

                alc.Client = BddCmd.Data.ClientExtensions.GetByKey(dx.Client, "P.I.C");
                alc.Intitule = textBox1.Text;
                alc.Departement = BddCmd.Data.DepartementExtensions.GetByKey(dx.Departement, "01");
                alc.Region = BddCmd.Data.RegionExtensions.GetByKey(dx.Region, "CENTRE");
                alc.Pay = BddCmd.Data.PayExtensions.GetByKey(dx.Pay, 40);


                try
                {
                    alc.Save(dx);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

PS:我做了反射以避免任何空值

1 个答案:

答案 0 :(得分:0)

问题主要发生在列映射到多个属性上。 在这种情况下,我认为问题出在[piece]列中。

您正在使用它作为ID和集合的关键

<id name="Piece" column="[piece]" type="Int32" >
     <generator class="assigned" />
</id>             
<bag name="LignecommandeclientList" inverse="true" cascade="all-delete-orphan" lazy="true" >
     <key>
         <column name="[piece]" />
     </key>
     <one-to-many class="Lignecommandeclient" />
</bag>

在一个包中,默认密钥是实体ID,因此您可以尝试从包中删除密钥节点。

<bag name="LignecommandeclientList" inverse="true" cascade="all-delete-orphan" lazy="true" >
     <one-to-many class="Lignecommandeclient" />
</bag>
祝你好运。

相关问题