0
votes

I am working on Mapping Spatial geometry(Point) On Postgresql to C# Code I need something like example in this question like example in this question but using Postgresql Instead of SQL Server

I did the following

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");
    }
}

And tried to use UserTypeConvention as below

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)");
    }
}

The configuration of the Fluent NHibernate is

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();
    }
}

When Try to run below

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();
            }
        }

    }

It give me exception when run Database.OpenSession() as below:

FluentNHibernate.Cfg.FluentConfigurationException: "An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail."

Inner Exception: "An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail."

Loader Exception: System.IO.FileLoadException Could not load file or assembly 'GeoAPI, Version=1.7.4.0, Culture=neutral, PublicKeyToken=a1a0da7def465678' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"GeoAPI, Version=1.7.4.0, Culture=neutral, PublicKeyToken=a1a0da7def465678

But I didn't use GeoAPI in any c# classes I created

1

1 Answers

0
votes

I assume that you are using the Point geometry type from the NetTopologySuite namespace? If so, the GeoAPI library is one of its dependencies.

How are you currently referencing NetTopologySuite in your project? If you are not already using NuGet, I would highly recommend it since it will automatically take care of installing all the necessary dependencies for you (see here for the NTS NuGet page).