SqlParameterCollection异常。 NHibernate的

时间:2012-02-27 10:58:27

标签: c# nhibernate exception

我收到Invalid index 22 for this SqlParameterCollection with Count=22.个例外。我知道这是一个常见的问题。我也知道,几乎每次它都与映射中的重复相关联。但是,我是否仍然不太了解映射,或者这是由于另一个原因引起的任何帮助!

ENTITY-映射

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BlackOakAccounting.DomainLayer" namespace="BlackOakAccounting.DomainLayer.Entities">
<class name="Employee" optimistic-lock="version">
    <id name="ID" column="EmployeeID">
        <generator class="guid.comb" />
    </id>
    <version name="Version" />
    <!-- properties -->
    <property name="EmployeeNumber" />
    <property name="TradeUnionNumber" unique="true" />
    <property name="Post" not-null="true" />
    <property name="Department" not-null="true" />
    <property name="DateOfHire" not-null="true" />        
    <property name="FirstName" not-null="true" />
    <property name="MiddleName" not-null="true" />
    <property name="LastName" not-null="true" />
    <property name="DateOfBirth" not-null="true" />
    <property name="SSN" not-null="true" unique="true" />
    <!-- components -->
    <component name="Address" class="BlackOakAccounting.DomainLayer.Components.Address">
        <property name="Country" />
        <property name="State" />
        <property name="City" not-null="true" />
        <property name="Street" not-null="true" />
        <property name="Appartment" />
        <property name="PostalCode" />
    </component>
    <component name="Passport" class="BlackOakAccounting.DomainLayer.Components.Attachment">
        <property name="Description" />
        <property name="ServerPath" />
    </component>
    <component name="EducationCertificates" class="BlackOakAccounting.DomainLayer.Components.Attachment">
        <property name="Description" />
        <property name="ServerPath" />
    </component>
    <component name="MaritalCertificates" class="BlackOakAccounting.DomainLayer.Components.Attachment">
        <property name="Description" />
        <property name="ServerPath" />
    </component>
    <component name="ChildBirthCertificates" class="BlackOakAccounting.DomainLayer.Components.Attachment">
        <property name="Description" />
        <property name="ServerPath" />
    </component>
    <component name="PersonalContact" class="BlackOakAccounting.DomainLayer.Components.Contact">
        <property name="EMail" not-null="true" />
        <property name="Phone" not-null="true" />
    </component>
    <component name="BusinessContact" class="BlackOakAccounting.DomainLayer.Components.Contact">
        <property name="EMail" not-null="true" />
        <property name="Phone" not-null="true" />
    </component>
</class>

ENTITY类

[Serializable]
public abstract class AbstractEntity<T> where T : AbstractEntity<T>
{
    private Nullable<Int32> hashCode;        

    public virtual Guid ID
    {
        get;
        private set;
    }
    public virtual Int32 Version
    {
        get;
        set;
    }

    public override Boolean Equals(Object obj)
    {
        var other = obj as T;

        if(other == null)
        {
            return false;
        }
        if(Equals(this.ID, Guid.Empty) && Equals(other.ID, Guid.Empty))
        {
            return ReferenceEquals(this, other);
        }
        return ID.Equals(other.ID);
    }
    public override Int32 GetHashCode()
    {
        if(this.hashCode.HasValue)
        {
            return this.hashCode.Value;
        }
        if(Equals(this.ID, Guid.Empty))
        {
            this.hashCode = base.GetHashCode();
            return this.hashCode.Value;
        }
        return this.ID.GetHashCode();
    }

    public static bool operator ==(AbstractEntity<T> lhs, AbstractEntity<T> rhs)
    {
        return Equals(lhs, rhs);
    }
    public static bool operator !=(AbstractEntity<T> lhs, AbstractEntity<T> rhs)
    {
        return !Equals(lhs, rhs);
    }
}

[Serializable]
public class Employee : AbstractEntity<Employee>, IAggregateRoot
{
    public Employee()
    {
        this.Address = new Address();

        this.PersonalContact = new Contact();
        this.BusinessContact = new Contact();

        this.EducationCertificates = new Attachment();
        this.MaritalCertificates = new Attachment();
        this.ChildBirthCertificates = new Attachment();
        this.Passport = new Attachment();
    }

    // names
    public virtual String FirstName { get; set; }
    public virtual String MiddleName { get; set; }
    public virtual String LastName { get; set; }
    public virtual String SSN { get; set; }

    // related to company
    public virtual String EmployeeNumber { get; set; }
    public virtual String TradeUnionNumber { get; set; }
    public virtual String Department { get; set; }
    public virtual String Post { get; set; }
    public virtual DateTime DateOfHire { get; set; }
    public virtual Contact BusinessContact { get; set; }

    // documentation paths
    public virtual Attachment EducationCertificates { get; set; }
    public virtual Attachment MaritalCertificates { get; set; }
    public virtual Attachment ChildBirthCertificates { get; set; }
    public virtual Attachment Passport { get; set; }

    // miscaleneous
    public virtual DateTime DateOfBirth { get; set; }
    public virtual Address Address { get; set; }
    public virtual Contact PersonalContact { get; set; }        
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经明白我错了。所以每个

<property name="Description" />
<property name="ServerPath" />

属性对我需要指定一个唯一的列名,因为组件没有通过表映射,而字段只是重复!