简体   繁体   English

如何在使用 db 函数的 ServiceStack.OrmLite 中添加自定义字段类型?

[英]How to add custom field type in ServiceStack.OrmLite which uses db function?

How custom field types can be used which calls db extension functions?如何使用调用 db 扩展函数的自定义字段类型? In this case PostGIS .在这种情况下PostGIS

From PostGIS install page slightly altered:从 PostGIS 安装页面略有改动:

CREATE TABLE 
  mytable ( 
    id SERIAL PRIMARY KEY,
    geom GEOMETRY(POINT, 26910)
  )
; 

INSERT INTO 
  mytable (geom) 
VALUES 
  (ST_GeomFromText('POINT(0 0)', 26910))
;

SELECT 
  id
FROM 
  mytable
WHERE 
  ST_DWithin(geom, ST_GeomFromText('POINT(0 0)', 26910), 1000)
;

How this table is generated in code?这个表是如何在代码中生成的? And how it is queried?以及如何查询?

class mytable
{
  [AutoIncrement]
  [PrimaryKey]
  public int id;

  [???]
  public ??? geom;
}

Related SO question: How to define 'geography' type using Npgsql and OrmLite (using postgresql, postgis, c#)相关 SO 问题: 如何使用 Npgsql 和 OrmLite 定义“地理”类型(使用 postgresql、postgis、c#)

Rather an old question but it came up for me when I was looking at this so I thought I would add this in case it helps others.而是一个老问题,但是当我看到这个问题时,它出现在我身上,所以我想我会添加这个,以防它对其他人有帮助。 Note that there may well be a better way to do this in ServiceStack now as it has massively increased functionality since the original question请注意,现在在 ServiceStack 中可能有更好的方法来执行此操作,因为自原始问题以来,它的功能已大大增加

I haven't yet been able to get this working using ServiceStack (the geometry fields return as null) but I have using Npgsql as follows.我还没有能够使用 ServiceStack(几何字段返回为空)使这个工作,但我使用 Npgsql 如下。

SELECT - use ::Text as follows - SELECT - 使用 ::Text 如下 -

       var conn = new NpgsqlConnection(connectionString);
        conn.Open();

        var cmd = new NpgsqlCommand(
            "select  id, geom :: TEXT from mytable", conn);

INSERT - use a standard insert as follows - INSERT - 使用标准插入如下 -

         var conn = new NpgsqlConnection(connectionString);
        conn.Open();

        using (var cmd = new NpgsqlCommand(
            "INSERT INTO mytable( geom) VALUES ( st_geomfromtext ( 'POINT(0,0)':: TEXT, 1000))",
            conn)){cmd.ExecuteNonQuery()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM