Entity Framework Code First (7) Spatial Data Type Spatial Data Types

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.