.net から Postgresql に接続して動作させるソフトを作っているのですが
Postgresql には POSTGIS をインストールしていたが、どうにもこうにも、geometryデータの取得が出来ない
エラーになるか、データが0か…
と言う訳で、確認した所、
URL:https://github.com/npgsql/npgsql/issues/807#issuecomment-236825653
本文:
PostGIS is supported at the ADO.NET level, but the EF6 provider doesn't support it yet. The relevant issue is npgsql/EntityFramework6.Npgsql#18. Note that EF Core already supports the PostGIS types.
適当な訳:
PostGSIは、ADO.NET では対応しているが、EF6(Entity Framework6)では対応していない。
EF Core(Entity Framework Core) では対応してるから、そっち使ってね
との事でした…orz
今使っているのが、.net Framework 4.5 …
EF Core Postgresql のインストールは .net Framework 4.5.1 以上…
使えないやんっ!!!orz
古いバージョンなら使えるのかなぁ…
と言う訳で、Entity Framework を使う場合、実行環境が許せば 【4.5.1以上 + EF Core】を使うべきである(6よりも新しいしね)
因みにEF6 で、
・【PostgisXXXX】系の構造体(変数宣言)を使うと、下記のエラーが表示されます。
PostgisPoint、PostgisLineString、PostgisPolygon等々
例外メッセージ:
【An error occurred while preparing the command definition. See the inner exception for details.】
詳細メッセージ:
【error 3004: Problem in mapping fragments starting at line XX:No mapping specified for properties クラス名.プロパティ名 in Set クラス名.
An Entity with Key (PK) will not round-trip when:
Entity is type [名前空間.クラス名]】
・【PostgisGeometry】構造体(変数宣言)を使うと、下記のエラーが表示されます。
例外メッセージ:
【値を Null にすることはできません。】
【パラメーター名:entitySet】
・【NpgsqlXXXX】系の構造体(変数宣言)を使うと、データが未設定「0,0」になります。
NpgsqlPoint、NpgsqlLine等々
→ こっちはGeometryじゃ無い全く違う型ので当たり前と言えば当たり前ですね…
EF6のgeometryデータが取れないパターン(geometry以外は取れます)
Table:
Entity:
context:
検索用:Entity Framework 6 Entity Framework Core EF6 EF Core POSTGIS Postgresql PostgisGeometry 値を Null パラメーター名:entitySet
Postgresql には POSTGIS をインストールしていたが、どうにもこうにも、geometryデータの取得が出来ない
エラーになるか、データが0か…
と言う訳で、確認した所、
URL:https://github.com/npgsql/npgsql/issues/807#issuecomment-236825653
本文:
PostGIS is supported at the ADO.NET level, but the EF6 provider doesn't support it yet. The relevant issue is npgsql/EntityFramework6.Npgsql#18. Note that EF Core already supports the PostGIS types.
適当な訳:
PostGSIは、ADO.NET では対応しているが、EF6(Entity Framework6)では対応していない。
EF Core(Entity Framework Core) では対応してるから、そっち使ってね
との事でした…orz
今使っているのが、.net Framework 4.5 …
EF Core Postgresql のインストールは .net Framework 4.5.1 以上…
使えないやんっ!!!orz
古いバージョンなら使えるのかなぁ…
と言う訳で、Entity Framework を使う場合、実行環境が許せば 【4.5.1以上 + EF Core】を使うべきである(6よりも新しいしね)
因みにEF6 で、
・【PostgisXXXX】系の構造体(変数宣言)を使うと、下記のエラーが表示されます。
PostgisPoint、PostgisLineString、PostgisPolygon等々
例外メッセージ:
【An error occurred while preparing the command definition. See the inner exception for details.】
詳細メッセージ:
【error 3004: Problem in mapping fragments starting at line XX:No mapping specified for properties クラス名.プロパティ名 in Set クラス名.
An Entity with Key (PK) will not round-trip when:
Entity is type [名前空間.クラス名]】
・【PostgisGeometry】構造体(変数宣言)を使うと、下記のエラーが表示されます。
例外メッセージ:
【値を Null にすることはできません。】
【パラメーター名:entitySet】
・【NpgsqlXXXX】系の構造体(変数宣言)を使うと、データが未設定「0,0」になります。
NpgsqlPoint、NpgsqlLine等々
→ こっちはGeometryじゃ無い全く違う型ので当たり前と言えば当たり前ですね…
EF6のgeometryデータが取れないパターン(geometry以外は取れます)
Table:
CREATE TABLE geodata.test ( id text NOT NULL, the_geom geometry(Point,4326), CONSTRAINT test_pkey PRIMARY KEY (id) )
Entity:
[Table("test", Schema = "geodata")] public class TestGeo { /// <summary> /// ID /// </summary> [Key] [Column("id", Order = 1)] public String Id { get; set; } /// <summary> /// 位置情報 /// </summary> [Column("the_geom")] public PostgisPointTheGeom { get; set; } }
context:
using DataBaseController.DBConnection.Entity.Geodatas; using log4net; using System; using System.Collections.Generic; using System.Data.Entity; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace aiueo.Context.Geodatas { public class GeodataContext : DbContext { private static ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public DbSet<TestGeo> TestGeo { get { return Set<TestGeo>(); } } /// <summary> /// コンストラクタ /// </summary> /// <param name="ip">接続先IPアドレス(ホスト名)</param> /// <param name="port">接続先ポート番号</param> /// <param name="dbName">接続先DB名称</param> /// <param name="userId">接続ID</param> /// <param name="password">接続パスワード</param> /// <param name="defaultSchema">接続先スキーマ</param> public GeodataOfficeExportContext(String ip, String port, String dbName, String userId, String password, String defaultSchema = "geodata") : base(GetConnecting(ip, port, dbName, userId, password), true) { // schemaの指定 DefaultSchema = defaultSchema; Configuration.ValidateOnSaveEnabled = false; Configuration.UseDatabaseNullSemantics = true; Database.Log = (log) => logger.Debug(log); } private static NpgsqlConnection GetConnecting(String ip, String port, String dbName, String userId, String password) { String connectString = String.Format(" server={0};port={1};database={2};user id={3};password={4};", ip, port, dbName, userId, password); logger.Info(connectString); return new NpgsqlConnection(connectString); } public void AccessData(short Id) { TestGeo.Where(e => e.Id == Id).ToList(); } } }
検索用:Entity Framework 6 Entity Framework Core EF6 EF Core POSTGIS Postgresql PostgisGeometry 値を Null パラメーター名:entitySet