代码之家  ›  专栏  ›  技术社区  ›  epitka

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

  •  1
  • epitka  · 技术社区  · 15 年前

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

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

    但引发了“System.IndexAutoFrangeException:此计数为9的SqlParameterCollection的索引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 回复  |  直到 12 年前
        1
  •  2
  •   Stamppot    12 年前

    也可以使用公式:

    Map(x => x.MyProperty).Formula("propertyColumn").Not.Insert().Not.Update();
    
        2
  •  0
  •   epitka    15 年前

    因为这是在视图上映射的,所以我在视图中添加了一个具有不同名称的列映射到同一列,并且它可以工作。