Training
Module
Use a database with minimal API, Entity Framework Core, and ASP.NET Core - Training
Learn how to add a database to a minimal API application.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 Entity Framework Code First. It also demonstrates how to use a LINQ query to find a distance between two locations.
This walkthrough will use Code First to create a new database, but you can also use Code First 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 Entity Framework Code First. 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.
When using Code First development you usually begin by writing .NET Framework classes that define your conceptual (domain) model. The code below defines the University class.
The University has the Location property of the DbGeography type. To use the DbGeography type, you must add a reference to the System.Data.Entity assembly and also add the System.Data.Spatial using statement.
Open the Program.cs file and paste the following using statements at the top of the file:
using System.Data.Spatial;
Add the following University class definition to the Program.cs file.
public class University
{
public int UniversityID { get; set; }
public string Name { get; set; }
public DbGeography Location { get; set; }
}
In addition to defining entities, you need to define a class that derives from DbContext and exposes DbSet<TEntity> properties. The DbSet<TEntity> properties let the context know which types you want to include in the model.
An instance of the DbContext derived type manages the entity objects during run time, which includes populating objects with data from a database, change tracking, and persisting data to the database.
The DbContext and DbSet types are defined in the EntityFramework assembly. We will add a reference to this DLL by using the EntityFramework NuGet package.
Note, that in addition to the EntityFramework assembly, a reference to the System.ComponentModel.DataAnnotations assembly is also added.
At the top of the Program.cs file, add the following using statement:
using System.Data.Entity;
In the Program.cs add the context definition.
public partial class UniversityContext : DbContext
{
public DbSet<University> Universities { get; set; }
}
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 UniversityContext ())
{
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.
When you run the application the first time, the Entity Framework creates a database for you. Because we have Visual Studio 2012 installed, the database will be created on the LocalDB instance. By default, the Entity Framework names the database after the fully qualified name of the derived context (in this example that is SpatialCodeFirst.UniversityContext). The subsequent times the existing database will be used.
Note, that if you make any changes to your model after the database has been created, you should use Code First Migrations to update the database schema. See Code First to a New Database for an example of using Migrations.
To view the database and data, do the following:
In this walkthrough we looked at how to use spatial types with Entity Framework Code First.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Use a database with minimal API, Entity Framework Core, and ASP.NET Core - Training
Learn how to add a database to a minimal API application.