DevForce provides limited support for EF 5 spatial types. Spatial types are supported in 2-tier and n-tier .NET 4.5 applications: they are not supported in Silverlight, Windows Store or Windows Phone applications - platforms where the CLR types in System.Data.Spatial are not defined.
Spatial types - DbGeography and DbGeometry - were introduced in Entity Framework 5 and can be used in applications targeting .NET 4.5.
Spatial types can be used in both Code First and EDMX-based models.
Let's look at a simple example, based on the EF documentation.
We define a simple Code First entity with a property of DbGeography type:
C# | [ProvideEntityAspect] public class University { public int UniversityID { get; set; } public string Name { get; set; } public DbGeography Location { get; set; } } |
The EntityManager looks like this:
C# | public class SpatialTestEntityManager : EntityManager { public EntityQuery<University> Universities { get; set; } } |
We can now create and save these entities:
C# | var mgr = new SpatialTestEntityManager(); mgr.AddEntity(new University() { Name = "Graphic Design Institute", Location = DbGeography.FromText("POINT(-122.336106 47.605049)") }); mgr.AddEntity(new University() { Name = "School of Fine Art", Location = DbGeography.FromText("POINT(-122.335197 47.646711)") }); mgr.SaveChanges(); |
And we can query them:
C# | var myLocation = DbGeography.FromText("POINT(-122.296623 47.640405)"); var university = (from u in mgr.Universities orderby u.Location.Distance(myLocation) select u).FirstOrDefault(); Console.WriteLine("The closest University to you is: {0}.", university.Name); |
We can also use spatial types in an EDMX-based model. This sample is also based on EF documentation.
We'll point our EDMX to the database created in our Code First sample above, but you can instead take a Model First approach to create the University entity we saw above.
Note the type of the Location property is Geography:
The generated code for the Location property:
C# | /// <summary>Gets or sets the Location. </summary> [DataMember] [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name="Location", AutoGenerateField=true)] public System.Data.Spatial.DbGeography Location { get { return PropertyMetadata.Location.GetValue(this); } set { PropertyMetadata.Location.SetValue(this, value); } } |
You can query and save these entities as usual.
C# | var mgr = new SpatialTestEntityManager(); mgr.AddEntity(new University() { Name = "Graphic Design Institute", Location = DbGeography.FromText("POINT(-122.336106 47.605049)") }); mgr.AddEntity(new University() { Name = "School of Fine Art", Location = DbGeography.FromText("POINT(-122.335197 47.646711)") }); mgr.SaveChanges(); var myLocation = DbGeography.FromText("POINT(-122.296623 47.640405)"); var university = (from u in mgr.Universities orderby u.Location.Distance(myLocation) select u).FirstOrDefault(); Console.WriteLine("The closest University to you is: {0}.", university.Name); |
C# | public class SpatialKnwonTypeProvider : IdeaBlade.EntityModel.IKnownTypeProvider { public IEnumerable<Type> AddKnownTypes() { return new[] { typeof(System.Data.Spatial.DbGeography) }; } } |