Entity Framework Spatial Data Type - Microsoft Entity Framework

What is Entity Framework Spatical Data Type?

Spatial type maintain was introduced in Entity Framework 5. A set of operators is also integrated to permit queries to analyze spatial data. For instance, a query can filter based on the distance between two geographic location.

  • Entity Framework will agree to new spatial data types to be showing as property on your classes and map them to spatial columns in your database.
  • You will also be able to write LINQ queries that make use of the spatial operators to filter, sort, and group based on spatial calculation performed in the database.

There are two most important spatial data types −

  • The geography data type stores ellipsoidal data, for example, GPS latitude and longitude coordinates.
  • The geometry data type represent Euclidean (flat) coordinate system.

Let’s take a look into the following instance of Cricket ground.

Step 1 − Create new project from File → New → Project menu option.

Step 2 − In the left panel select the Console Application.

Step 3 − Right-click on project name and select Manage NuGet Packages…


Step 4 − Install Entity Framework.

Step 5 − Add reference to System.Data.Entity meeting and also add the System.Data.Spatial using statement for spatial data types.

Step 6 − Add the following class in Program.cs file.

Step 7 − In addition to significant entities, you need to define a class that derives from DbContext and exposes DbSet<TEntity> properties.

In the Program.cs add the context explanation.

Step 8 − Add the following code into the Main function, which will add two new CricketGround things to the context.

Spatial properties are initialized by using the DbGeography.FromText method. The geography point represent as WellKnownText is passed to the method and then saves the data. After that Cricket Ground object will be retrieve where its location is closest to the particular location.
When the above code is execute, you will accept the following output –

We recommend that you execute the above example in a step-by-step method for better accepting.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Microsoft Entity Framework Topics