【发布时间】:2013-02-28 00:54:21
【问题描述】:
我一直在尝试让 NHibernate ICompositeUserType 映射工作。但我坚持尝试使实现足够通用以在不同的表上使用。
我们的旧数据库有许多带有纬度和经度的表,我想将它们作为位置类映射到我的域对象中。问题是每个表的纬度和经度列都有不同的名称。
我已经创建了 ICompositeUserType 的实现,但我似乎必须将 PropertyNames 设置为表中列的名称,这意味着我不能在不同的表(具有不同的列名)上使用 CustomType。
我认为我应该将 PropertyNames 设置为我的 Position 类中的属性名称,并将它们映射到我的 ClassMap 中的表列,但这似乎引发了 NHibernate 异常:
NHibernate.MappingException: property mapping has wrong number of columns
感觉好像我在映射中做错了什么,但我无法弄清楚。
这是来自我的 ICompositeUserType 的一个 sn-p 代码:
public class PositionUserType : ICompositeUserType
{
private readonly IType[] _propertyTypes =
new [] { NHibernateUtil.Double , NHibernateUtil.Double };
private readonly string[] _propertyNames =
new[] { "Latitude", "Longitude" };
public string[] PropertyNames { get { return _propertyNames; } }
public IType[] PropertyTypes { get { return _propertyTypes; } }
// Other methods omitted
}
还有我的班级地图:
public class LastPositionMap : ClassMap<LastPosition>
{
public LastPositionMap()
{
Map(p => p.Position)
.Columns.Add("LPLongitude", "LPLongitude")
.CustomType(typeof(PositionUserType));
// Other mapping omitted
}
}
和职位类
public class Position
{
public double Latitude { get; private set; }
public double Longitude { get; private set; }
public Position(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
}
我目前有一个可以使用 Component fluent map 的工作,但这意味着我的 Position 类必须是可变的,如果不是,我更喜欢它。
有人可以帮忙吗?我已经很好地查看了几篇文章和书籍,但我似乎仍然无法让它发挥作用。
谢谢,
亚当
【问题讨论】:
-
遇到与您发布的问题相同的问题,使用 FNH 清除映射没有帮助,HBM 文件看起来正确。
标签: nhibernate fluent-nhibernate