我上映射空間幾何(點)在PostgreSQL上到C#代碼工作,我需要這樣的東西例如,在這個問題like example in this question但地圖PostGIS的點對點在C#中使用功能NHibernate
我沒有使用而不是SQL Server的PostgreSQL的下面
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)");
}
}
的功能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,文化=中立, 公鑰= a1a0da7def465678' 或。它的一個依賴的 位於組件的清單定義不(從HRESULT異常:0x80131040)匹配組件 參考。 「:」 GeoAPI, 版本= 1.7.4.0,文化=中性公鑰= a1a0da7def465678
但我沒有在我創建的任何c#類中使用GeoAPI