Spatial Data Type Support in Entity Framework 5

Entity Framework 5 brings many improvements and Spatial Data Type Support in Code First is one of them.
 
In this article, I'll use simple steps to develop a Console Application with Entity Framework Code First and then will explore Spatial Data Types.
 
Step 1: Create a New Project
 
Create a new console application File > New > Project > Visual C# > Console Application.
 
Step 2: Install EF5 from NuGet or Package Manager
 
At very first, we need to install the Entity Framework 5 for this console project from NuGet Package Manager. For this, in "Solution Explorer" right-click on the project and click on "Manage NuGet Packages" and install Entity Framework 5.
 
1.jpg
 
Alternatively, you can install this package from the "Package Manager Console" using the command "Install-Package EntityFramework", this will install the latest version of Entity Framework in your project.
 
2.jpg
 
Step 3: Understanding Spatial Data Types
 
In Entity Framework 5, there are two main spatial data types: geography and geometry. The geography data type stores ellipsoidal data (for example, GPS latitude and longitude coordinates). The geometry data type represents a Euclidean (flat) coordinate system. More here.
 
Step 4: Conceptual Model and DbContext
 
In Code First development we usually begin by writing .NET Framework classes that define our conceptual (domain) model and the code below defines the "Student" class.
 
The "Student" model has the "Location" property of the DbGeography type. To use the DbGeography type, we must add a reference to the "System.Data.Entity" assembly and also add the "System.Data.Spatial" using statement.
 
In addition to defining entities, we need to define a class that derives from DbContext and exposes DbSet<T> properties. The DbSet<T> properties let the context know which types you want to include in the model.
  1. public partial class Student  
  2. {  
  3.     public int ID { getset; }  
  4.     public string Name { getset; }  
  5.     public string Address { getset; }  
  6.     public DbGeography Location { getset; }  
  7. }  
  8.    
  9. public partial class Student_Spatial : DbContext  
  10. {  
  11.     public DbSet Students { getset; }  

Step 5: Adding and selecting data
 
Use the following code to find the closest student:
  1. using (var context = new Student_Spatial())  
  2. {  
  3.     Student student1 = new Student()  
  4.     {  
  5.         Name = "Abhimanyu K Vatsa",  
  6.         Address = "Bokaro",  
  7.         Location = DbGeography.FromText("POINT(23.684774 86.914565)")  
  8.     };  
  9.     Student student2 = new Student()  
  10.     {  
  11.         Name = "Deepak Kumar",  
  12.         Address = "Bokaro",  
  13.         Location = DbGeography.FromText("POINT(23.563453 86.876875)")  
  14.     };  
  15.     context.Students.Add(student1);  
  16.     context.Students.Add(student2);  
  17.     context.SaveChanges();  
  18.    
  19.     var myCurrLocation = DbGeography.FromText("POINT(23.445342 86.108453)");  
  20.    
  21.     var stdQuery = (from i in context.Students  
  22.                     orderby i.Location.Distance(myCurrLocation)  
  23.                     select i).FirstOrDefault();  
  24.    
  25.     Console.WriteLine("The closest student to me is " + stdQuery.Name);  
  26.     Console.ReadKey();  

In the above code, I'll be adding two student records in the database and with each record, I have the Location property that will be using DbGeography Spatial Data Type.
 
Step 6: Complete Code
 
And the complete code of the demo is:
  1. using System;  
  2. using System.Linq;  
  3. using System.Data.Entity;  
  4. using System.Data.Spatial;  
  5.    
  6. namespace ConsoleApplication5_Spatial  
  7. {  
  8.     public partial class Student  
  9.     {  
  10.         public int ID { getset; }  
  11.         public string Name { getset; }  
  12.         public string Address { getset; }  
  13.         public DbGeography Location { getset; }  
  14.     }  
  15.    
  16.     public partial class Student_Spatial : DbContext  
  17.     {  
  18.         public DbSet Students { getset; }  
  19.     }  
  20.    
  21.     class Program  
  22.     {  
  23.         static void Main(string[] args)  
  24.         {  
  25.             using (var context = new Student_Spatial())  
  26.             {  
  27.                 Student student1 = new Student()  
  28.                 {  
  29.                     Name = "Abhimanyu K Vatsa",  
  30.                     Address = "Bokaro",  
  31.                     Location = DbGeography.FromText("POINT(23.684774 86.914565)")  
  32.                 };  
  33.                 Student student2 = new Student()  
  34.                 {  
  35.                     Name = "Deepak Kumar",  
  36.                     Address = "Bokaro",  
  37.                     Location = DbGeography.FromText("POINT(23.563453 86.876875)")  
  38.                 };  
  39.                 context.Students.Add(student1);  
  40.                 context.Students.Add(student2);  
  41.                 context.SaveChanges();  
  42.    
  43.                 var myCurrLocation = DbGeography.FromText("POINT(23.445342 86.108453)");  
  44.    
  45.                 var stdQuery = (from i in context.Students  
  46.                                 orderby i.Location.Distance(myCurrLocation)  
  47.                                 select i).FirstOrDefault();  
  48.    
  49.                 Console.WriteLine("The closest student to me is " + stdQuery.Name);  
  50.                 Console.ReadKey();  
  51.             }  
  52.         }  
  53.     }  

Step 7: Using the Model Designer
 
If you want to do the same using the "Entity Model Designer", or call it you are a Mode Designer then, just add a new "Scalar Property" in your conceptual Model and by selecting "Scalar Property" in the model change its "Type" property from "Property Explorer", find one screen here.
 
1_designer.png
 
So, use whatever workflow you love.
 
I hope you like this article. Thanks.


Similar Articles