Entity Framework - Spatial Data Type


Spatial type support was introduced in Entity Framework 5. A set of operators is also included to allow queries to analyze spatial data. For example, a query can filter based on the distance between two geographic locations.

  • Entity Framework will allow new spatial data types to be exposed as properties on your classes and map them to spatial columns in your database.

  • You will also be able to write LINQ queries that make use of the spatial operators to filter, sort, and group based on spatial calculations performed in the database.

There are two main spatial data types −

  • The geography data type stores ellipsoidal data, for example, GPS latitude and longitude coordinates.

  • The geometry data type represents Euclidean (flat) coordinate system.

Let’s take a look into the following example of Cricket ground.

Step 1 − Create new project from File → New → Project menu option.

Step 2 − In the left pane, select the Console Application.

Cricket Project

Step 3 − Right-click on project name and select Manage NuGet Packages…

NuGet Project

Step 4 − Install Entity Framework.

Step 5 − Add reference to System.Data.Entity assembly and also add the System.Data.Spatial using statement for spatial data types.

Add Reference

Step 6 − Add the following class in Program.cs file.

public class CricketGround {
   public int ID { get; set; }
   public string Name { get; set; }
   public DbGeography Location { get; set; }
}

Step 7 − In addition to defining entities, you need to define a class that derives from DbContext and exposes DbSet<TEntity> properties.

In the Program.cs add the context definition.

public partial class CricketGroundContext : DbContext {
   public DbSet<CricketGround> CricketGrounds { get; set; }
}

Step 8 − Add the following code into the Main function, which will add two new CricketGround objects to the context.

class Program {

   static void Main(string[] args) {

      using (var context = new CricketGroundContext()) {

         context.CricketGrounds.Add(new CricketGround() {
            Name = "Shalimar Cricket Ground", 
            Location = DbGeography.FromText("POINT(-122.336106 47.605049)"), 
         });

         context.CricketGrounds.Add(new CricketGround() {
            Name = "Marghazar Stadium", Location = DbGeography
               .FromText("POINT(-122.335197 47.646711)"), 
         });

         context.SaveChanges();

         var myLocation = DbGeography.FromText("POINT(-122.296623 47.640405)");

         var cricketGround = (from cg in context.CricketGrounds
            orderby cg.Location.Distance(myLocation) select cg).FirstOrDefault();

         Console.WriteLine("The closest Cricket Ground to you is: {0}.", cricketGround.Name);
      }
   }
}

Spatial properties are initialized by using the DbGeography.FromText method. The geography point represented as WellKnownText is passed to the method and then saves the data. After that CricketGround object will be retrieved where its location is closest to the specified location.

When the above code is executed, you will receive the following output −

The closest Cricket Ground to you is: Marghazar Stadium

We recommend that you execute the above example in a step-by-step manner for better understanding.

Advertisements