4

我需要访问父类的 ApplicationID 属性才能运行 Nhibernate 查询。在 NUnit 中运行此查询的测试会导致它失败:“BusinessObjects.IntegrationTests.CMSEligibilityInfoRepositoryTest.FetchListByApplicationIDTest: System.IndexOutOfRangeException : Invalid index 6 for this SqlParameterCollection with 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;
        }
    }
4

1 回答 1

10

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

于 2012-04-24T21:15:48.973 回答