带有Count的此SqlParameterCollection的索引无效

时间:2012-04-19 19:05:57

标签: c# fluent-nhibernate nunit fluent-nhibernate-mapping

我需要访问父类的ApplicationID属性才能运行Nhibernate查询。在NUnit中对此查询运行测试会导致它失败:“BusinessObjects.IntegrationTests.CMSEligibilityInfoRepositoryTest.FetchListByApplicationIDTest:System.IndexOutOfRangeException:此SqlParameterCollection的索引6无效,Count = 6.”

在尝试获取ApplicationID之前,此代码集合正常工作。

到目前为止,我所做的最好的研究是它是一个映射问题。

家长班:

public abstract class MemberEligibilityInfo
{
    #region Access Properties

    private int _managedBenOpID;
    private int _managedApplicationID;

    /// <summary>
    /// ID
    /// </summary>
    public virtual uint ID { get; set; }

    /// <summary>
    /// MemberElig ID that will work with NHibernate
    /// </summary>
    public virtual int ManagedMemberEligID { get; set; }

    /// <summary>
    /// Member ID
    /// </summary>
    public virtual ulong MemberID { get; set; }

    /// <summary>
    /// Member ID that will work with NHibernate
    /// </summary>
    public virtual long ManagedMemberID { get; set; }

    /// <summary>
    /// Benefit Option ID
    /// </summary>
    public virtual uint BenefitOptionID { get; set; }

    public virtual int ManagedBenOpID
    {
        get { return _managedBenOpID; }
        set
        {
            if (_managedBenOpID == value)
            {
                BenefitOptionID = (uint)_managedBenOpID;
                return;
            }

            _managedBenOpID = value;
            BenefitOptionID = (uint)_managedBenOpID;
        }
    }

    /// <summary>
    /// Benefit Option
    /// </summary>
    public virtual string UBOI { get; set; }

    /// <summary>
    /// Application ID
    /// </summary>
    public virtual uint ApplicationID { get; set; }

    public virtual int ManagedApplicationID
    {
        get { return _managedApplicationID; }

        set
        {
            if (_managedApplicationID == value)
            {
                ApplicationID = (uint)_managedApplicationID;
                return;
            }

            _managedApplicationID = value;
            ApplicationID = (uint)_managedApplicationID;
        }
    }
    /// <summary>
    /// Application Plan Year date.
    /// </summary>
    public virtual DateTime ApplicationPlanYear { get; set; }

    /// <summary>
    /// Effective Date.
    /// </summary>
    public virtual DateTime EffectiveDate { get; set; }

    /// <summary>
    /// Termination Date.
    /// </summary>
    public virtual DateTime TermDate { get; set; }

    #endregion // Access Properties

    #region Constructors

    /// <summary>
    /// Default constructor.
    /// </summary>
    public MemberEligibilityInfo()
    {
        ID = 0;
        MemberID = 0;
        BenefitOptionID = 0;
        UBOI = string.Empty;
        ApplicationID = 0;
        ApplicationPlanYear = DateTime.MinValue;
        EffectiveDate = DateTime.MinValue;
        TermDate = DateTime.MinValue;
    }

    #endregion // Constructors
}

儿童班:

public class CMSEligibilityInfo : MemberEligibilityInfo
{
    private BenefitOptionInfo _managedBenefitOptionInfo;

    #region Access Properties

    /// <summary>
    /// Precedence
    /// </summary>
    public virtual int Precedence { get; set; }

    /// <summary>
    /// Is Active
    /// </summary>
    public virtual bool Active { get; set; }


    public virtual BenefitOptionInfo ManagedBenefitOptionInfo
    {
        get { return _managedBenefitOptionInfo; }
        set
        {
            if (_managedBenefitOptionInfo == value)
            {
                return;
            }

            _managedBenefitOptionInfo = value;
            this.ManagedApplicationID = ManagedBenefitOptionInfo.ManagedApplicationID;
        }
    }

    public virtual MemberInfo MemberInfo
    {
        get;
        set;
    }

    #endregion // Access Properties

    #region Constructors

    /// <summary>
    /// Default Constructor
    /// </summary>
    public CMSEligibilityInfo() : base()
    {
        Precedence = 0;
        Active = false;
    }

    #endregion // Constructors
}

子类映射:

public class CMSEligibilityInfoMap : ClassMap<CMSEligibilityInfo>
{
    public CMSEligibilityInfoMap()
    {
        // Table
        Table("_MEI_EligPeriods");

        // Unique Identifier
        Id(x => x.ManagedMemberEligID, "MemEligID")
            .GeneratedBy.Identity();

        // Member ID, Managed to handle the bigint
        Map(x => x.ManagedMemberID, "MemberID")
            .Not.Nullable();

        // Benefit Option ID
        Map(x => x.ManagedBenOpID, "BenOpID")
            .Not.Nullable();

        //// Effective Date
        Map(x => x.EffectiveDate, "Eff")
            .Nullable();

        // Termination Date
        Map(x => x.TermDate, "Term")
            .Nullable();

        // Is the eligibility record active
        Map(x => x.Active, "Active")
            .Not.Nullable();

        Map(x => x.Precedence, "Precedence")
            .Nullable();

        References(x => x.ManagedBenefitOptionInfo)
            .Column("BenOpID")
            .Not.LazyLoad()
            .Cascade.None();

        // References back to MemberInfo
        References(x => x.MemberInfo)
            .Column("MemberID")
            .Not.LazyLoad()
            .Cascade.None();
    }
}

在导致错误的存储库方法中查询:

    /// <summary>
    /// Fetch a list of eligibility info based on Application ID
    /// </summary>
    /// <param name="appID">Selected Application ID</param>
    /// <returns>A list of eligibility information</returns>
    public IList<CMSEligibilityInfo> FetchListByApplicationID(uint? appID)
    {
        using (var session = SessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            var list = session.CreateCriteria<CMSEligibilityInfo>()
                .CreateAlias("ManagedBenefitOptionInfo", "ManagedBenefitOptionInfo")                 .Add(Restrictions.Eq(Projections.Property("ManagedBenefitOptionInfo.ManagedApplicationID"), appID))
                .List<CMSEligibilityInfo>();

            // Commit the transaction
            transaction.Commit();

            return list;
        }
    }

1 个答案:

答案 0 :(得分:9)

事实证明,这种错误是由同一属性绑定两次造成的,或者更确切地说,映射了两个具有相同名称的属性。我们删除了父对象(因为它从未使用过)并创建了子基类,并确保BenefitOptionInfo对象是唯一通过BenOpID映射的对象。