【问题标题】:Map Postgis Point to Point in C# using Fluent NHibernate使用 Fluent NHibernate 在 C# 中映射 Postgis 点到点
【发布时间】:2017-07-01 10:05:38
【问题描述】:

我正在将 Postgresql 上的空间几何(点)映射到 C# 代码我需要类似这个问题 like example in this question 中的示例,但使用 Postgresql 而不是 SQL Server

我做了以下

public class Student
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Point Location { get; set; }
}

public class StudentMap : ClassMap<Student>
{
    public StudentMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Location).CustomType<PostGisPointUserTypeConvention>();
        Table("Student");
    }
}

并尝试使用 UserTypeConvention 如下

public class PostGisPointUserTypeConvention : UserTypeConvention<PostGisGeometryType>
{
    public override void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(c => c.Type == typeof(Point));
    }

    public override void Apply(IPropertyInstance instance)
    {
        // Have to set CustomType to be able to read/write rows using NHibernate
        instance.CustomType<PostGisGeometryType>();
        // Have to set CustomSqlType to generate correct SQL schema
        instance.CustomSqlType("geometry(Point)");
    }
}

Fluent NHibernate的配置是

public class Database
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
                _sessionFactory = InitializeSessionFactory(); return _sessionFactory;
        }
    }

    /// <summary>
    /// This Function working fine with Map class in the projects
    /// </summary>
    /// <returns></returns>
    public static ISessionFactory InitializeSessionFactory()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["PostgreConnectionString"].ConnectionString;
        IPersistenceConfigurer config = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString);

        FluentConfiguration configuration = Fluently
            .Configure()
            .Database(config)
            .Mappings(m =>
                m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));
        configuration.ExposeConfiguration(x => x.SetProperty("hbm2ddl.keywords", "auto-quote"));
        return configuration.BuildSessionFactory();
    }
    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

当尝试在下方运行时

public void GET()
    {
        using (var session = Database.OpenSession())
        {
            // populate the database
            using (var transaction = session.BeginTransaction())
            {
                Point loc = new Point(38.690993, 38.690993);
                var student = new Student
                {
                    Name = "Nehal",
                    Age = 20,
                    Location = loc
                };

                session.Save(student);
                transaction.Commit();
            }
        }

    }

运行 Database.OpenSession() 时出现异常,如下所示:

FluentNHibernate.Cfg.FluentConfigurationException:“无效或 创建 SessionFactory 时使用了不完整的配置。 检查 PotentialReasons 集合和 InnerException 了解更多信息 详细。”

内部异常:“使用了无效或不完整的配置 在创建 SessionFactory 时。检查PotentialReasons集合, 和 InnerException 了解更多详细信息。”

加载程序异常:System.IO.FileLoadException 无法加载文件或 程序集'GeoAPI,版本=1.7.4.0,文化=中性, PublicKeyToken=a1a0da7def465678' 或其依赖项之一。这 定位程序集的清单定义与程序集不匹配 参考。 (来自 HRESULT 的异常:0x80131040)“:”GeoAPI, 版本=1.7.4.0,文化=中性,PublicKeyToken=a1a0da7def465678

但我没有在我创建的任何 c# 类中使用 GeoAPI

【问题讨论】:

    标签: c# postgresql fluent-nhibernate postgis fluent-nhibernate-mapping


    【解决方案1】:

    我假设您正在使用来自 NetTopologySuite 命名空间的 Point 几何类型?如果是这样,GeoAPI 库就是它的依赖项之一。

    您目前在项目中如何引用NetTopologySuite?如果您还没有使用 NuGet,我强烈推荐它,因为它会自动为您安装所有必要的依赖项(请参阅 here 获取 NTS NuGet 页面)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 2011-06-18
      • 2010-12-09
      • 2012-04-03
      • 1970-01-01
      相关资源
      最近更新 更多