NHibernate:如何将同一列映射为属性和关系
我尝试使用以下映射将同一列映射为属性和关系(出于与遗留数据有关的原因):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以使用公式:
You can also use Formula:
由于这是映射到视图上的,因此我在视图中又添加了一列,并将不同的名称映射到同一列,并且它有效。
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.