NHibernate:如何将同一列映射为属性和关系
我正在尝试使用以下映射将同一列映射为属性和关系(原因与遗留数据有关):NHibernate:如何将同一列映射为属性和关系,nhibernate,fluent-nhibernate,nhibernate-mapping,orm,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,Orm,我正在尝试使用以下映射将同一列映射为属性和关系(原因与遗留数据有关): References(x => x.BaseProductTemplate, "ProductCodeTxt"); Map(x => x.DescriptionCode, "ProductCodeTxt") .CustomType(typeof(TrimmedStringUserType)); 但是“System.IndexOutOfRangeException:计数为9的此
References(x => x.BaseProductTemplate, "ProductCodeTxt");
Map(x => x.DescriptionCode, "ProductCodeTxt")
.CustomType(typeof(TrimmedStringUserType));
但是“System.IndexOutOfRangeException:计数为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);
}
}
公共静态营销计划BaseProduct创建(BaseProductTemplate BaseProductTemplate,ProductType ProductType)
{
var toReturn=新的营销计划基础产品();
toReturn.BaseProductTemplate=BaseProductTemplate;
toReturn.Type=产品类型;
回归回归;
}
受保护的营销计划基础产品()
{
_共同保险百分比=新列表();
_福利=新列表();
}
#区域“旧系统解决方案”
///黑客:在insight中,用户能够覆盖描述和代码,系统显示来自的描述
///“BaseProduct”表,而不是“ProductRef”表。以便在Insight中显示此说明
///过渡时期
///我们正在用两个独立属性对MarketingPlanBase产品进行建模
///将根据“ProductCodeTxt”和ProductNameTxt中的值加载。
///然而,新的MarketingPlanBaseProducts将根据BaseProductTemplate(“ProductRef”)填充描述
///代码/说明将无法从新系统更改
///一旦旧系统被切断,“DescriptionCode”属性应被删除,
///“名称应更改为从BaseProductTemplate关系派生值的映射属性
私有字符串_descriptionCode;
公共虚拟字符串描述符代码
{
得到
{
返回描述代码;
}
}
私有字符串\u名称;
公共虚拟字符串名
{
获取{return\u name;}
}
私有void SetName(BaseProductTemplate BaseProductTemplate)
{
_name=baseProductTemplate.name;
_descriptionCode=baseProductTemplate.Code;
}
私有BaseProductTemplate\u BaseProductTemplate;
公共虚拟BaseProductTemplate BaseProductTemplate
{
得到
{
返回_baseProductTemplate;
}
专用设备
{
_baseProductTemplate=值;
SetName(_baseProductTemplate);
}
}
因为这是在视图上映射的,所以我在视图中添加了一个列,该列的不同名称映射到了同一列,并且可以正常工作。您还可以使用公式:
Map(x => x.MyProperty).Formula("propertyColumn").Not.Insert().Not.Update();
这是Fluent NHibernate映射。您能否向我们提供您的域模型,以便我们更好地了解您希望在此处描述的关系?