NHibernate:如何将同一列映射为属性和关系

时间:2010-03-10 18:40:41

标签: nhibernate fluent-nhibernate nhibernate-mapping orm

我正在尝试使用以下映射将同一列映射为属性和关系(出于与遗留数据有关的原因):

 References(x => x.BaseProductTemplate, "ProductCodeTxt");
 Map(x => x.DescriptionCode, "ProductCodeTxt")
                .CustomType(typeof(TrimmedStringUserType));

但是“System.IndexOutOfRangeException:此SqlParameterCollection的索引9无效,Count = 9。”抛出异常。如何在没有出现此错误的情况下使用NH实现此目的。

这是一个班级:

    public static MarketingPlanBaseProduct Create(BaseProductTemplate baseProductTemplate, ProductType productType)
        {
            var toReturn = new MarketingPlanBaseProduct();

            toReturn.BaseProductTemplate = baseProductTemplate;
            toReturn.Type = productType;


            return toReturn;
        }

        protected MarketingPlanBaseProduct()
        {
            _coInsurancePercentages = new List<PlanCoInsurance>();
            _benefits = new List<BaseProductBenefit>();
        }


  #region " Old system workaround "

        /// HACK: In insight users were able to override description and the code, and system was displaying description from 
        /// the "BaseProduct" table, not from "ProductRef" table. In order to have this description display in Insight 
        /// during transitional period
        /// we are modeling the MarketingPlanBaseProduct with two independent properties 
        /// that will be loaded based on the values in "ProductCodeTxt" and ProductNameTxt.
        /// New MarketingPlanBaseProducts will however have description populated based on BaseProductTemplate ("ProductRef")
        /// and code/description will not be changable from New System
        /// Once old system is cut off, "DescriptionCode" Property should be removed,
        /// "Name should be changed to be mapped property that derives value from BaseProductTemplate relationship

        private string _descriptionCode;
        public virtual string DescriptionCode
        {
            get
            {
                return _descriptionCode;
            }
        }

        private string _name;
        public virtual string Name
        {
            get { return _name; }
        }

        private  void SetName(BaseProductTemplate baseProductTemplate)
        {
            _name = baseProductTemplate.Name;
            _descriptionCode = baseProductTemplate.Code;
        }

     private BaseProductTemplate _baseProductTemplate;
        public virtual BaseProductTemplate BaseProductTemplate
        {
            get
            {
                return _baseProductTemplate;
            }
            private set
            {
                _baseProductTemplate = value;
                SetName(_baseProductTemplate);
            }
        }

2 个答案:

答案 0 :(得分:2)

您也可以使用公式:

Map(x => x.MyProperty).Formula("propertyColumn").Not.Insert().Not.Update();

答案 1 :(得分:0)

由于这是在视图上映射的,我在视图中添加了一个列,其中不同的名称映射到同一列,并且它可以工作。