entity-framework/ef6/modeling/designer/data-types/spatial.md
[!NOTE] EF5 Onwards Only - The features, APIs, etc. discussed in this page were introduced in Entity Framework 5. If you are using an earlier version, some or all of the information does not apply.
The video and step-by-step walkthrough shows how to map spatial types with the Entity Framework Designer. It also demonstrates how to use a LINQ query to find a distance between two locations.
This walkthrough will use Model First to create a new database, but the EF Designer can also be used with the Database First workflow to map to an existing database.
Spatial type support was introduced in Entity Framework 5. Note that to use the new features like spatial type, enums, and Table-valued functions, you must target .NET Framework 4.5. Visual Studio 2012 targets .NET 4.5 by default.
To use spatial data types you must also use an Entity Framework provider that has spatial support. See provider support for spatial types for more information.
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.
This video shows how to map spatial types with the Entity Framework Designer. It also demonstrates how to use a LINQ query to find a distance between two locations.
Presented By: Julia Kornich
You will need to have Visual Studio 2012, Ultimate, Premium, Professional, or Web Express edition installed to complete this walkthrough.
The Entity Designer, which provides a design surface for editing your model, is displayed.
The wizard performs the following actions:
[!NOTE] When you build, warnings about unmapped entities and associations may appear in the Error List. You can ignore these warnings because after we choose to generate the database from the model, the errors will go away.
Now we can generate a database that is based on the model.
Open the Program.cs file where the Main method is defined. Add the following code into the Main function.
The code adds two new University objects to the context. Spatial properties are initialized by using the DbGeography.FromText method. The geography point represented as WellKnownText is passed to the method. The code then saves the data. Then, the LINQ query that that returns a University object where its location is closest to the specified location, is constructed and executed.
using (var context = new UniversityModelContainer())
{
context.Universities.Add(new University()
{
Name = "Graphic Design Institute",
Location = DbGeography.FromText("POINT(-122.336106 47.605049)"),
});
context.Universities.Add(new University()
{
Name = "School of Fine Art",
Location = DbGeography.FromText("POINT(-122.335197 47.646711)"),
});
context.SaveChanges();
var myLocation = DbGeography.FromText("POINT(-122.296623 47.640405)");
var university = (from u in context.Universities
orderby u.Location.Distance(myLocation)
select u).FirstOrDefault();
Console.WriteLine(
"The closest University to you is: {0}.",
university.Name);
}
Compile and run the application. The program produces the following output:
The closest University to you is: School of Fine Art.
To view data in the database, right-click on the database name in SQL Server Object Explorer and select Refresh. Then, click the right mouse button on the table and select View Data.
In this walkthrough we looked at how to map spatial types using the Entity Framework Designer and how to use spatial types in code.