ActiveRecord and NHibernate Spatial



我在使用ActiveRecord时遇到了一些问题。

好吧,一切都很好,但有时也会起作用。不是所有的时间。

当我试图导航到包含空间实体的项目中引用的MVC页面时(只有一个空间实体,而这个实体没有空间类型(,我会遇到这个异常。

{"已声明GeometryType列,但未配置空间方言"}

有一种方言配置正确。我试着用两种方式配置它:Xml和InPlace。

这是我的启动方法:

    public static void StartActiveRecord()
    {
        IDictionary<string,string> hash = new Dictionary<string,string>();
        hash.Add("isWeb", "true");
        hash.Add("connection.driver_class","NHibernate.Driver.NpgsqlDriver");
        hash.Add("connection.connection_string","Server=localhost;Port=5432;database=nhiber;User ID=postgres;Password=pass;");
hash.Add("connection.provider","NHibernate.Connection.DriverConnectionProvider");
            hash.Add("dialect","NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial.PostGis");
            hash.Add("proxyfactory.factory_class","NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
        InPlaceConfigurationSource source = new InPlaceConfigurationSource();
        source.Add(typeof(ActiveRecordBase), hash);
        ActiveRecordStarter.Initialize(source, GetActiveRecordTypes());
        foreach (Configuration cfg in ActiveRecordMediator.GetSessionFactoryHolder().GetAllConfigurations())
        {
            cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
            //Metadata.AddMapping(cfg, MetadataClass.GeometryColumn);
            //Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem);
        }
    }

这是我在Global.asax 中的启动方法

    protected void Application_Start()
    {
        Ignition.StartActiveRecord();
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
    }

这种错误有时会发生。杀死开发服务器有时会让它正常,但几步后又会崩溃。

救命!

编辑:我正在添加到这个和其他一些信息的映射

当有方言时,会在Global.asax上的Ignition.StartActiveRecord((中出错。当没有方言时,则会在ActiveRecordStarter.Initialize((中出现错误;

可以肯定的是,下面映射的这个对象是整个程序集中唯一具有空间意识的对象。

public class Complaint:ActiveRecordBase<Complaint>
{
[PrimaryKey(Column="complaint_id",Generator=PrimaryKeyType.Sequence,SequenceName="complaint_seq")]
        public virtual int ComplaintId { get; set; }
        [Property(Column="date_of_complaint",NotNull=true)]
        public virtual DateTime DateOfComplaint { get; set; }
        [Property(Column="description",Length=256,NotNull=true)]
        public virtual string Description { get; set; }
        [Property(Column="complaint_status",NotNull=true,Default="1")]
        public cityzenComplaintStatus Status { get; set; }
        [BelongsTo(Column = "complaint_type_id")]
        public ComplaintType Type { get; set; }
        [Property("the_geom", ColumnType = "NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial")]
        public virtual IGeometry Geometry { get; set; }
        [OneToOne(ForeignKey="official_answer_id")]
        public virtual OfficialAnswer CityAnswer { get; set; }
        [BelongsTo("user_id", Fetch = FetchEnum.Select, Lazy = FetchWhen.OnInvoke, Update = false, NotNull = true)]
        public virtual CityzenUser User { get; set; }
        [HasMany(typeof(Vote),Table="vote",ColumnKey="complaint_id",RelationType=RelationType.Set,Inverse=true)]
        public virtual IList<Vote> Votes { get; set; }
        [HasMany(typeof(Comment),Table="comment",ColumnKey="complaint_id",RelationType=RelationType.Set,Inverse=true)]
        public virtual IList<Comment> Comments { get; set; }
        [Property(Column = "deleted", Default = "false", NotNull = true)]
        public virtual bool Deleted { get; set; }
}

您的配置对我来说很合适,但无论出于什么原因,每当NH实例化GeometryType的实例时,它都找不到您配置使用的方言,这样它就知道您需要初始化什么类型的IGeometry(在您的情况下是PostGisGeometryType(。

作为一个临时的解决方法,你可以这样声明你的成员变量:

[Property("the_geom", ColumnType = "NHibernate.Spatial.Type.PostGisGeometryType, NHibernate.Spatial.PostGis")]
public virtual IGeometry Geometry { get; set; }

如果我发现了什么,我会寄回这里。

这是对的吗?搞砸添加("方言","NHibernat.Spatial.Distople.PostGisDialect,NHibernate.SSpatial.PostGis"(;不应该是这样的:搞砸添加("方言","NHibernat.Spatial.Distlect.PostGisDialect,NHibernate.SSpatial"(;

看一眼:http://nhibernate.info/doc/spatial/configuration-and-mapping.html

最新更新