Disclaimer: This article focuses onEF5+,Visual Studio 2012 +
Spatial Data Type (Spatial Data Types) Is inEF5Introduced in, there are two types of spatial data:
- Geography(Geographic)-stores elliptical data, suchGPSThe longitude and latitude coordinates;
- Geometry(Ry)-represents the coordinate system of the Euclidean ry (plane.
The following example shows an application of this data type.
Create a C # console application
BelieveConsoleAll should be created and namedSpatialCodeFirst
Use Code First to create a Model
Note thatDbGeographyAddSystem. Data. Entity. SpatialNamespace
UniversityID { ; Name { ; DbGeography Location { ;
Define the derived type of DbContext
InstallEntity Framework(SeeHttp://www.cnblogs.com/panchunting/p/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application.html)
Note: Add a namespaceSystem. Data. Entity, System. Data. Entity. ModelConfiguration. Conventions
UniversityContext() : ( DbSet<University> Universities { ; <PluralizingTableNameConvention>
Define UniversityInitializer
Note: Add a namespaceSystem. Data. Entity, System. Data. Entity. Spatial
UniversityInitializer : DropCreateDatabaseIfModelChanges<UniversityContext> universities = List<University> University { Name = , Location = DbGeography.FromText( University { Name = , Location = DbGeography.FromText(=>
Configure App. config
Pay attention to the yellow highlighted part. The first part is to configure the database connection (UseLocalDB), The second part isDbContextAnd its initiators
Add code call in Program. cs
Main( ( context = myLocation = DbGeography.FromText( university = ( u
Run to view results
Background Database Definition
Table Data
(Localdb) \ v11.0 is not in the list
Right-click NewConnectionYou can.
Original article:Http://msdn.microsoft.com/en-us/data/hh859721