Fluent Nhibernate problem (ClassMap)

烈酒焚心 提交于 2019-12-03 10:19:49

问题


I have the following XML (.hbm):

<property name="Geometry" column="the_geom">
   <type name="NHibernate.Spatial.Type.GeometryType,NHibernate.Spatial">
      <param name="subtype">MULTIPOLYGON</param>
      <param name="srid">-1</param>
   </type>
</property>

It´s using Nhibernate Spatial type... How can I map that property using ClassMap (Fluent Nhibernate) ?

Thanks


回答1:


Well, I've not used NHibernate Spatial, but I browsed through the code and it looks like GeometryType inherits from IUserType so you should be able to use it with .CustomTypeIs<>

For example:

Map(x => x.Geometry, "the_geom").CustomTypeIs<GeometryType>();

Unless it happens automagically, that probably won't get you your param elements though. I'm not sure of a truly nice way to do this but you can always add an XML alteration like so:

Map(x => x.Geometry, "the_geom").AddAlteration(p => p.AddElement("type")
    .WithAtt("name", "NHibernate.Spatial.Type.GeometryType,NHibernate.Spatial")
        .AddElement("param")
            .WithAtt("name", "subtype")
            .WithText("MULTIPOLYGON")
        .ParentNode
        .AddElement("param")
            .WithAtt("name", "srid")
            .WithText("-1")
    );

Note that to get the WithText functionality, you'll have to add an extension for XmlElement like so (WithAtt and AddElement are extensions in the FluentNHibernate.Mapping namespace):

public static class XmlExtensions
{
    public static XmlElement WithText(this XmlElement element, string text)
    {
        element.InnerText = text;
        return element;
    }
}


来源:https://stackoverflow.com/questions/723050/fluent-nhibernate-problem-classmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!