此 SqlParameterCollection 的索引无效

本文关键字:无效 索引 SqlParameterCollection | 更新日期: 2023-09-27 18:31:36

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

在尝试获取应用程序 ID 之前,此代码集合工作正常。

到目前为止,我的研究得出的最好的结果是,这是一个映射问题。

父类:

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;
        }
    }

此 SqlParameterCollection 的索引无效

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