Entity Framework - Spatial Datatypes

Entity Framework supports working with spatial data through the DbGeography or DbGeometry classes. These classes rely on database-specific functionality provided by the Entity Framework provider. All providers does not support spatial datatypes.

Support for spatial types was introduced in EF5.in EF5 spatial types are only supported when the application targets and runs on .NET 4.5.
EF6 spatial types are supported for applications targeting both .NET 4 and .NET 4.5. Click Here for more detail.

MS SQL Server 2008 introduced two spatial data types, geography and geometry. 

Entity Framework - Spatial DataTypes

There are two main spatial data types: geography and geometry. 
Geography: The geography data type stores ellipsoidal data (ie, GPS latitude and longitude coordinates). 
Geometry: The geometry data type represents Euclidean (flat) coordinate system.

Spatial type support was introduced in Entity Framework 5. Entity Framework supports spatial data types DbGeography and DbGeometry.  

We have added two more column in DU_FND_Message table
Column Name        DataType
1. Location              geography
2. Gem                    geometry

Now update EDM designer by right clicking. After updating EDM you can see the types of Location and Gem property of DU_FND_Message entity are System.Data.Spatial.DBGeography and System.Data.Spatial.DbGeometry

Step 1 - Create new project from File and select the Console Application? New ? Project menu option.

Entity Framework - Spatial Datatypes in Database

Step 2 - Right-click on project name and select Manage NuGet Packages…Install Entity Framework.

EntityFramework - Install EntityFramework

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

EntityFramework - Add Reference

Step 4 - Add the following class in Program.cs file.

EntityFramework - Add Class

Step 5 - 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.

EntityFramework - Add Context Class

Step 6 - Add the following code into the Main function, which will add two new HockeyGround objects to the context.

EnityFramework - Add Main Class

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 HockeyGround 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 nearest Ground is: XXX

Execute the above example in a step-by-step manner for better understanding.

Praesent mattis

Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio vel elit.

Read More