This is the seventh article in the "Windows Phone Mango Local Database (SQLCE)" series. To get you started using the database in Windows Phone Mango, this series of short film articles will cover all the knowledge you need to know. I'll talk about mapping classes to tables in the database, that is, mapping between your object model and the database schema.
1. What is Database mapping?in order to create a local database, first, you must define an entity class. These classes define your object model and its mapping to the database schema. The object-relational capabilities of LINQ to SQL depend on the details of the mapping to create a relational database to map to the corresponding DataContext. for each entity, specify the details of the mapping by using LINQ to SQL mapping. These attributes specify the attributes of the database such as tables, columns, primary keys, and indexes. You can view the http://msdn.microsoft.com/zh-cn/library/bb386971.aspx for more information.
Reference : You can take a look at the MSDN documentation: http://msdn.microsoft.com/zh-cn/library/hh202860 (v=vs.92). aspx2. Understanding Database Mappinguse attribute mapping types to tables, attributes to columns:(1) in the database in order to map the entity class to the table, it can be considered that the entity class is [table] attribute(2) [Index]attribute can be used to define an index (on a class)(3) in the database, in order to map entity attributes to columns, you can assume that the entity attribute is [column] attribute.
[Column (IsPrimaryKey = True)] is
used to specify the primary key attribute. Use [association] attribute to specify the association, which allows you to configure the relationship between two tables/entities in a database map. Association attribute has the following important properties:
- The name of the Otherkey-property corresponds to the ID of the object at the other end of the association
- The Thiskey-property's name corresponds to the primary key on the type
- Support variables for the Storage-property
This version of Windows Phone Mango uses code-first methods to define the database schema as a preferred option, i.e. there is no visual designer available to help developers map and configure their classes to process the database. Or at least it is not officially supported. This means that you must:1. Configuring the properties of a map2. Write down all the classes yourself3. If the database does not exist, write code to create it4, if you want to have to upgrade the old database version to a new feature, you must write those code again