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.
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.
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.
- public partial class Student
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public DbGeography Location { get; set; }
- }
-
- public partial class Student_Spatial : DbContext
- {
- public DbSet Students { get; set; }
- }
Step 5: Adding and selecting data
Use the following code to find the closest student:
- using (var context = new Student_Spatial())
- {
- Student student1 = new Student()
- {
- Name = "Abhimanyu K Vatsa",
- Address = "Bokaro",
- Location = DbGeography.FromText("POINT(23.684774 86.914565)")
- };
- Student student2 = new Student()
- {
- Name = "Deepak Kumar",
- Address = "Bokaro",
- Location = DbGeography.FromText("POINT(23.563453 86.876875)")
- };
- context.Students.Add(student1);
- context.Students.Add(student2);
- context.SaveChanges();
-
- var myCurrLocation = DbGeography.FromText("POINT(23.445342 86.108453)");
-
- var stdQuery = (from i in context.Students
- orderby i.Location.Distance(myCurrLocation)
- select i).FirstOrDefault();
-
- Console.WriteLine("The closest student to me is " + stdQuery.Name);
- 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:
- using System;
- using System.Linq;
- using System.Data.Entity;
- using System.Data.Spatial;
-
- namespace ConsoleApplication5_Spatial
- {
- public partial class Student
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public DbGeography Location { get; set; }
- }
-
- public partial class Student_Spatial : DbContext
- {
- public DbSet Students { get; set; }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- using (var context = new Student_Spatial())
- {
- Student student1 = new Student()
- {
- Name = "Abhimanyu K Vatsa",
- Address = "Bokaro",
- Location = DbGeography.FromText("POINT(23.684774 86.914565)")
- };
- Student student2 = new Student()
- {
- Name = "Deepak Kumar",
- Address = "Bokaro",
- Location = DbGeography.FromText("POINT(23.563453 86.876875)")
- };
- context.Students.Add(student1);
- context.Students.Add(student2);
- context.SaveChanges();
-
- var myCurrLocation = DbGeography.FromText("POINT(23.445342 86.108453)");
-
- var stdQuery = (from i in context.Students
- orderby i.Location.Distance(myCurrLocation)
- select i).FirstOrDefault();
-
- Console.WriteLine("The closest student to me is " + stdQuery.Name);
- Console.ReadKey();
- }
- }
- }
- }
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.
So, use whatever workflow you love.
I hope you like this article. Thanks.