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

发布于 2024-08-24 05:50:40 字数 2719 浏览 4 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知在何时 2024-08-31 05:50:40

您还可以使用公式:

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

You can also use Formula:

Map(x => x.MyProperty).Formula("propertyColumn").Not.Insert().Not.Update();
暮色兮凉城 2024-08-31 05:50:40

由于这是映射到视图上的,因此我在视图中又添加了一列,并将不同的名称映射到同一列,并且它有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文