Spatial Data Type Support in Entity Framework 5

Entity Framework 5 brings number of improvements and Spatial Data Type Support is one of them.

In this post I’ll follow the simple steps to develop Console Application with Entity Framework Code First and then will explore Spatial Data Types.

Step 1: Create 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 project and click on ‘Manage NuGet Packages’ and install Entity Framework 5.



Alternatively, you can install this package from ‘Package Manager Console’ by using command ‘Install-Package EntityFramework’, this will install 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 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 ‘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<Student> 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 above code I’ll be adding two students records in the database and with each record I’ve 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<Student> 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 Model Designer

If you want to do the same by using '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 model change its 'Type' property from 'Property Explorer', find one screen here.



So, use whatever workflow you love.

I hope you like this post. Thanks.

Comments

Popular posts from this blog