开发者

NHibernate: How to map same column to be an attribute and relationship

I am trying to map same column to be an attribute and a relationship (for reasons that have to do with legacy data) using following mapping:

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

but "System.IndexOutOfRangeException: Invalid index 9 for this SqlParameterCollection with Count=9." exception is thrown. How can I achieve this with NH without getting this error.

Here is a class:

    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 =开发者_StackOverflow 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);
            }
        }


You can also use Formula:

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


Since this was mapped on the view, I added one more column in the view with different name mapped to the same column, and it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜