Spatial Data Type in Entity Framework

MS SQL Server 2008 introduced two spatial data types, geography and geometry. The geography type represents data in a round-earth coordinate system and geometry represent data in a Euclidean (flat) coordinate system.

Entity Framework includes special data types in System.Data.Entity.Spatial namespace: DbGeography for geography data type and DbGeometry for geometry data type in SQL Server, since version EF 5.0.

For demo purposes, we have used the Location column of Course table of geography type in SQL Server database, as shown below:

Entity Framework 5.0 Tutorial

Now, create or update an entity data model (.edmx) after adding geography column in the database. After creating or updating EDM, you can see that the the Location property of Course entity is System.Data.Spatial.DBGeography, as shown below:

public partial class Course
    public Course()
        this.Students = new HashSet<Student>();
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public Nullable<int> TeacherId { get; set; }
    public System.Data.Spatial.DbGeography Location { get; set; }
    public virtual Teacher Teacher { get; set; }
    public virtual ICollection<Student> Students { get; set; }

You can now use the Location property while adding or updating an entity, as shown below.

using (var ctx = new SchoolDBEntities())
    ctx.Courses.Add(new Course() { 
                CourseName = "New Course", 
                Location = DbGeography.FromText("POINT(-122.360 47.656)") 


Visit MSDN for more information on geography data type and geometry data type of MS SQL Server.