Entity Framework中的空间数据类型
MS SQL Server 2008引入了两种空间数据类型,geography(地理数据)和geometry(几何数据)。geography表示圆地球坐标系中的数据,geometry表示欧几里得(平面)坐标系中的数据。
从EF 5.0版本开始,它在System.Data.Entity.Spatial命名空间中包含了特殊的数据类型:DbGeography用于地理数据类型,DbGeometry用于SQL Server中的几何数据类型。
为了演示目的,我们使用了SQL Server数据库中地理类型的课程表中的Location列,如下所示:
现在,在数据库中添加地理列之后,创建或更新实体数据模型(.edmx)。在创建或更新EDM之后,您可以看到Course实体的Location属性是System.Data.Spatial.DBGeography,如下所示:
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; }
}
现在,您可以在添加或更新实体时使用Location属性,如下所示。
using (var ctx = new SchoolDBEntities())
{
ctx.Courses.Add(new Course() {
CourseName = "New Course",
Location = DbGeography.FromText("POINT(-122.360 47.656)")
});
ctx.SaveChanges();
}
访问MSDN获取更多关于MS SQL Server中的地理数据类型和几何数据类型的信息。
参考
https://www.entityframeworktutorial.net/
https://msdn.microsoft.com/