【问题标题】:NHibernate Spatial Mapping returns Antrl errorNHibernate 空间映射返回 Antrl 错误
【发布时间】:2014-11-20 19:07:12
【问题描述】:

我正在使用 NHibernate 3.3。我有一个案例,我想插入一个未引用的计算列。

My Domain Entity 可以简化为表单

    public class Location
    {
         public virtual IPoint GeoLocation {get;set;}
    }

    public class MappingOverride : IAutoMappingOverride<Location>
    {
        public void Override(AutoMapping<Location> mapping)
        {
            mapping
              .Map(e => e.GeoLocation)
              .Column("GeoLocation")
              .CustomSqlType("geography")
               .CustomType<MsSql2008GeographyType>;
        }
    }

表格列的类型为“地理”

但它会出错

   at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
   at System.Reflection.RuntimeAssembly.GetExportedTypes()
   at GeoAPI.GeometryServiceProvider.GetLoadableTypes(Assembly assembly)
   at GeoAPI.GeometryServiceProvider.ReflectInstance()
   at GeoAPI.GeometryServiceProvider.get_Instance()
   at NetTopologySuite.Geometries.Geometry.set_SRID(Int32 value)

它说它需要Antrl.Runtime,但是一个非常旧的版本。所有Antrl.Runtime nuget 包都有不同的程序集标识符。

无法加载文件或程序集 'antlr.runtime, Version=2.7.6.2, Culture=neutral, PublicKeyToken=1790ba318ebc5d56' 或其依赖项之一。系统找不到指定的文件。

我在一个单独的项目中工作,我按照代码约定使用 map,它在没有任何参考 Antrl.Runtime 的情况下工作。

需要帮助为自己指明正确的方向...

【问题讨论】:

标签: nhibernate spatial fluent-nhibernate-mapping


【解决方案1】:

作为一个单独的示例项目,我尝试了一些可行的方法,但在实际的 ASP.NET MVC 项目中不起作用。

类的定义

public class Location : IEntity
{
    public virtual int Id { get; protected set; }

    public virtual string LocationName { get; set; }

    public virtual IPoint GeoLocation { get; set; }
}

映射

public class LocationMapping : IAutoMappingOverride<Location>
{
    public void Override(AutoMapping<Location> mapping)
    {
        mapping.Map(x => x.LocationName).Column("Name");
        mapping.Map(x => x.GeoLocation).Column("GeoLocation")
               .CustomType<MsSql2008GeographyType>();
    }
}

测试

public class WhenSavingGeoLocation : BasePersistanceTest
    {
        [Test]
        public void Save()
        {
            this.Session
             .SaveOrUpdate(new Location { 
                   LocationName = "Category 01", 
                   GeoLocation = new Point(-72.12, 32.2323234) {  SRID = 4326 } 
            });
        }
    }

NHibernate.Spatial 使用 NetTopologicalSuite,它本身是 GeoAPI 的派生词 GeoAPI 库定义了一个接口GeoAPI.Geometries,并且在加载/搜索当前项目中的正确实现时,由于 MVC 项目中的 Antlr 错误而失败。

然而,它能够在单独的示例项目中获取 NetTopologySuite.NtsGeometryServices 实现。 GeoAPI 中搜索实现的实际代码如下所示

       private static IGeometryServices ReflectInstance()
        {
#if !PCL
            var a = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in a)
            {
                // Take a look at issue 114: http://code.google.com/p/nettopologysuite/issues/detail?id=114
                if (assembly is System.Reflection.Emit.AssemblyBuilder) continue;
                if (assembly.GetType().FullName == "System.Reflection.Emit.InternalAssemblyBuilder") continue;
                if (assembly.GlobalAssemblyCache && assembly.CodeBase == Assembly.GetExecutingAssembly().CodeBase) continue;

                foreach (var t in GetLoadableTypes(assembly))
                {
                    if (t.IsInterface) continue;
                    if (t.IsAbstract) continue;
                    if (t.IsNotPublic) continue;
                    if (!typeof(IGeometryServices).IsAssignableFrom(t)) continue;

                    var constuctors = t.GetConstructors();
                    foreach (var constructorInfo in constuctors)
                    {
                        if (constructorInfo.IsPublic && constructorInfo.GetParameters().Length == 0)
                            return (IGeometryServices)Activator.CreateInstance(t);
                    }
                }
            }
#endif
            throw new InvalidOperationException("Cannot use GeometryServiceProvider without an assigned IGeometryServices class");
        }
    }

在几何体上设置 SRID 时会调用它。我猜我的 MVC 项目有一个对一个程序集的引用,这使得它看起来适合 Antlr。 有一些建议为较新的 Antlr 程序集添加重定向。我也试过了,但错误仍然不断重复。

【讨论】:

    猜你喜欢
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2019-10-22
    • 2014-12-15
    • 2021-02-17
    • 1970-01-01
    • 2019-01-05
    相关资源
    最近更新 更多