سؤال

Is it possible to use the Entity Framework to preform a sqlite intersect query on a geom datatype?

I have had success with the below SQLiteConnection (after loading the 'libspatialite-4.dll')

string query = @"SELECT * FROM Spatial_Table WHERE intersects(Geom, GeomFromText('LINESTRING (-69.929201602936 23.577301964831, -20.358889102936 37.730080532218, 4.6020483970643 55.586555530742)', 4326)) = 1"

I have had success using the EF to query non spatial data from my SQLite database.

Is this type of query possible? Can I load the dll?

Thanks very much

هل كانت مفيدة؟

المحلول 2

Unfortunately no.

Neither System.Data.SQLite provider nor devArt's dotConnect for SQLite provider support spatial (Geometry, Geography) types at this time.

نصائح أخرى

Bit late to the party I know (I only just spotted this) but the accepted answer is not strictly true.

What is true is that there are no Geometry and/or Geography types in EF to support this, but there is a way you can get around it.

First off...

You can load the extension DLL's using the ExecuteSqlCommand on your database context as follows:

_ctx.Database.ExecuteSqlCommand(@"SELECT load_extension('libspatialite-4.dll')");

Secondly, if you return your Geometry as binary rather than text, you can bring the data back from your DB as a regular

byte[] 

data type. This may seem a little counter productive until you have a nose around on NuGet and install the ".NET Topology Suite" (NTS for short)

You can feed these binary arrays into NTS and it will construct and return to you a first class .NET object representing your geometry. NTS basically has everything in it that Spatialite has but in a C# assembly. In fact, I would go so far as to say, with NTS you actually don't need spatialite, you can just save and retrieve binary arrays to / from your database and operate on them purely in your own code.

The only thing you lose doing things that way is the ability to do SQL like select statements to search geometry out of your DB.

Fear not however....

For that, you can return your results to your code as regular gegraphic text objects such as

POINT(1 2)

or

LINE(1 2,3 4,5 6,7 8)

All you need to do is make sure the result of any SQL querys you perform create thier output by passing the result through Spatialites "AsText" function.

So to use your example

string query = @"SELECT col1,col2,col3,AsText(GeometryColumn) FROM Spatial_Table WHERE intersects(Geom, GeomFromText('LINESTRING (-69.929201602936 23.577301964831, -20.358889102936 37.730080532218, 4.6020483970643 55.586555530742)', 4326)) = 1"

Which will produce something like:

1,2,3,POINT(1 2)
4,5,6,POINT(3 4)
1,2,3,POINT(1 2)
4,5,6,POINT(3 4)

Which you can feed into a normal .NET object as follows:

public class myclass
{
  public int col1 { get; set; }
  public int col2 { get; set; }
  public int col3 { get; set; }
  public string GeometryColumn { get; set; }
}

and once again, if you then employ NTS, NTS can parse and turn into a graphic object anything passed from Spatialite in the text string.

My Company uses these techniques very reliably in one of our flagship products and we have done since EF 4.3.

The ExecuteSqlCommand has just appeared in EF5, but there are other similar ways of executing arbitrary SQL statements in previous versions.

You'll also find that on your entity tables there is a new function (I believe added in EF5) called SqlQuery which allows you to run data reader style querys on a given table and make use of your strongly typed models, to get the data back.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top