nhibernate - 在具有不同列名的不同表上使用 ICompositeUserType

标签 nhibernate fluent-nhibernate

我一直在尝试让 NHibernate ICompositeUserType 映射工作。但是我一直在尝试使实现足够通用以用于不同的表。

我们的遗留数据库有许多包含纬度和经度的表,我想将它们作为 Position 类映射到我的域对象中。问题是每个表的纬度和经度列都有不同的名称。

我已经创建了 ICompositeUserType 的实现,但我似乎必须将 PropertyNames 设置为表中列的名称,这意味着我不能在不同的表(具有不同的列名)上使用 CustomType。

我认为我应该将 PropertyNames 设置为我的 Position 类中的属性名称,并将它们映射到我的 ClassMap 中的表列,但这似乎引发了一个 NHibernate 异常:

NHibernate.MappingException: property mapping has wrong number of columns

感觉好像我在映射中做错了什么,但我想不通。

这是我的 ICompositeUserType 中的一段代码:

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
}

和我的类(class) map :

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 类必须是可变的,如果不是的话我会更喜欢它。

有人可以帮忙吗?我已经仔细阅读了几篇文章和书籍,但我似乎仍然无法让它发挥作用。

谢谢,

亚当

最佳答案

在映射中添加列名覆盖之前,您需要清除 Column 集合:

public class LastPositionMap : ClassMap<LastPosition>
{
    public LastPositionMap()
    {
        Map(p => p.Position)

            // Clear the columns to get rid of Fluent NH's default column names
            .Columns.Clear()

            .ColumnsAdd("LPLongitude", "LPLongitude")
            .CustomType(typeof(PositionUserType));

        // Other mapping omitted
    }
}

如果您在添加自定义名称之前不清除列集合,Fluent NH 只会将新的列名称附加到用户类型映射的列集合中,这会导致您为给定的用户类型获得太多列.

如果您从流畅的映射中生成实际的 XML 映射(通过在流畅的配置中使用 Mappings.ExportTo()),您可能会看到如下内容:

<property <!-- details here -->
  <column name="Latitude" /> 
  <column name="Longitude" /> 
  <column name="LPLongitude" /> 
  <column name="LPLatitude" /> 
</property>

实际应该是什么时候:

<property <!-- details here -->
  <column name="LPLatitude" /> 
  <column name="LPLongitude" /> 
</property>

关于nhibernate - 在具有不同列名的不同表上使用 ICompositeUserType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2362853/

相关文章:

linq-to-sql - linq to entities vs fluent nhibernate vs linq to sql(帮助)

c# - 如何在 Fluent nHibernate 中对 HasMany 映射进行额外过滤?

c# - NHibernate 缓存问题

.net - 如何使用 FluentMigrator 设置 ROWVERSION/TIMESTAMP 数据类型列?

JoinQueryOver 中的 NHibernate QueryOver Sum

.net - 为什么并发 GC 有时会导致 ExecutionEngineException(根据 MSDN)?

nhibernate - 如何在NHibernate Queryover where子句中进行String.IsNullOrEmpty()测试?

Nhibernate Azure 性能

mysql - 使用 NHibernate 从带有外键的 MySQL 表中获取数据

fluent-nhibernate - 流利的NHibernate自引用多对多