- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 elppsoidal data, for example, GPS latitude and longitude coordinates.
The geometry data type represents Eucpdean (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 Apppcation.
Step 3 − Right-cpck 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.
pubpc class CricketGround { pubpc int ID { get; set; } pubpc string Name { get; set; } pubpc 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.
pubpc partial class CricketGroundContext : DbContext { pubpc 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 = "Shapmar 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 initiapzed 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