- Entity Framework Tutorial
- Entity Framework - Home
- Entity Framework - Overview
- Entity Framework - Architecture
- Entity F - Environment Setup
- Entity Framework - Database Setup
- Entity Framework - Data Model
- Entity Framework - DbContext
- Entity Framework - Types
- Entity Framework - Relationships
- Entity Framework - Lifecycle
- Entity F - Code First Approach
- Entity F - Model First Approach
- Entity F - Database First Approach
- Entity Framework - DEV Approaches
- Entity F - Database Operations
- Entity Framework - Concurrency
- Entity Framework - Transaction
- Entity Framework - Views
- Entity Framework - Index
- Entity F - Stored Procedures
- Entity F - Disconnected Entities
- Entity F - Table-Valued Function
- Entity Framework - Native SQL
- Entity Framework - Enum Support
- Entity F - Asynchronous Query
- Entity Framework - Persistence
- Entity F - Projection Queries
- Entity F - Command Logging
- Entity F - Command Interception
- Entity Framework - Spatial Data Type
- Entity Framework - Inheritance
- Entity Framework - Migration
- Entity Framework - Eager Loading
- Entity Framework - Lazy Loading
- Entity Framework - Explicit Loading
- Entity Framework - Validation
- Entity Framework - Track Changes
- Entity Framework - Colored Entities
- Entity F - Code First Approach
- Entity Framework - First Example
- Entity Framework - Data Annotations
- Entity Framework - Fluent API
- Entity Framework - Seed Database
- Entity F - Code First Migration
- Entity F - Multiple DbContext
- Entity F - Nested Entity Types
- Entity Framework Resources
- Entity Framework - Quick Guide
- Entity Framework - Useful Resources
- Entity Framework - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Step 3 − Right-click on project name and select Manage NuGet Packages…
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.
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.