C#,PostgreSQL映射空间点

时间:2017-02-11 11:09:15

标签: c# postgresql nhibernate fluent-nhibernate postgis

我是NHibernate的新手,Fluent NHibernate技术,需要在我的API类和postgresql(postgis)数据库之间进行映射我将从Code开始  我为Fluent Nhibernate创建了如下配置

public class Database
{
    private static ISessionFactory _sessionFactory;
    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
                _sessionFactory = InitializeSessionFactory(); return _sessionFactory;
        }
    }
    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())
                .Conventions.Add());
        configuration.ExposeConfiguration(x => x.SetProperty("hbm2ddl.keywords", "auto-quote"));
        return configuration.BuildSessionFactory();
    } public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

类代码是:

public class Student
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string LastName { get; set; }
    public virtual int Age { get; set; }
    public virtual IPoint Location { get; set; }
}

如下地图到类:

public StudentMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Age);
        Map(x => x.LastName).Column("last_name");
        Map(x => x.Location).CustomType("NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial");
        Table("Student");
    }

[更新] 我有例外,Fluent NHibernate对空间数据类型一无所知,我已经使用NHibernate xml配置配置它,如下所示

public static Configuration Configure()
    {
        var cfg = new Configuration();
        cfg.DataBaseIntegration(x =>
        {
            x.ConnectionString = connectionStringName;
            x.Driver<NpgsqlDriver>();
            x.Dialect<PostGisDialect>();
            x.LogSqlInConsole = true;

        });

        //cfg.Configure();
        cfg.AddAssembly(Assembly.GetExecutingAssembly());
        cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
        Metadata.AddMapping(cfg, MetadataClass.GeometryColumn);
        Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem);
        return cfg;
    }

但需要使用Fluent NHibernate进行相应的实现。

0 个答案:

没有答案