Windows Phone Mango Local Database(SQL CE): Database mapping
published on: 8/25/2011 | Views: N/A | Tags: LocalDB Mango
by WindowsPhoneGeek
This is the 7th post from the "Windows Phone Mango Local Database(SQL CE)" series of short posts that will cover all you need to know in order to get started using a Local Database in Windows Phone 7.1 Mango. This time I am going to talk about mapping classes to tables in the database, or in other words - the mapping between your object model and database schema.
Here is what else is included in this series:
- Windows Phone Mango Local Database(SQL CE): Introduction
- Windows Phone Mango Local Database(SQL CE): Linq to SQL
- Windows Phone Mango Local Database(SQL CE): [Table] attribute
- Windows Phone Mango Local Database(SQL CE): [Column] attribute
- Windows Phone Mango Local Database(SQL CE): [Association] attribute
- Windows Phone Mango Local Database(SQL CE): [Index] attribute
- Windows Phone Mango Local Database(SQL CE): Database mapping
- Windows Phone Mango Local Database(SQL CE): DataContext
- Windows Phone Mango Local Database(SQL CE): Connection Strings
- Windows Phone Mango Local Database(SQL CE): Creating the Database
- Windows Phone Mango Local Database(SQL CE): Database Queries with LINQ
- Windows Phone Mango Local Database(SQL CE): How to Insert data
- Windows Phone Mango Local Database(SQL CE): How to Update data
- Windows Phone Mango Local Database(SQL CE): How to Delete data
What is Database mapping?
To create a local database, you must first define the entity classes. These classes define your object model and its mapping to the schema of the database. The object-relational capabilities of LINQ to SQL depend on these mapping details to create a relational database that maps to the corresponding data context.
For each entity, mapping details are specified by using LINQ to SQL mapping attributes. These attributes specify database-specific features such as tables, columns, primary keys, and indexes. For more information, see Attribute-based Mapping (LINQ to SQL).
For reference you can also take a look at the MSDN documentation.
Understanding the Database mapping
Mapping types to tables and properties to columns in the database schema is done using attributes:
- Entity classes are attributed with a [Table] attribute, in order to map them to tables in the database
- The [Index] attribute can be used (on classes) to define an index
- Entity properties are attributed with a [Column] attribute, in order to map them to columns in the database
[Column( IsPrimaryKey = true )] is used to specify the primary key property
Associations are specified using the [Association] attribute, which allows you to configure a relation between two entities / tables in the database mapping. The Association attribute has the following important properties:
- OtherKey - the name of the property that corresponds to the id of the object at the other end of the association
- ThisKey - the name of the property that corresponds to the primary key for this type
- Storage - the backing variable for the property
The code-first approach to defining the database schema is preferred in this version of Windows Phone 7.1 Mango. I.e. at the moment there is no visual designer that can help developers in mapping and configuring their classes to work with the database. Or at least it is not supported officially.
This means that you will have to:
- Configure the mapping with attributes:
- Write all classes on your own
- Write the code that creates the database if it does not exists.
- If you want to have functionality that upgrades an old version of the database to a new one you will again have to write the code on your own.
In this article I talked about mapping your classes to database tables when working with Windows Phone Mango Local Database. Stay tuned for the rest of the posts.
You can also follow us on Twitter @winphonegeek
Comments
This is so awsome
posted by: Tony on 9/2/2011 9:09:46 PM
Just want to say, I can't wait for the rest to come about. Have any idea when,
•Windows Phone Mango Local Database(SQL CE): How to Insert data
Will be out?
RE:This is so awsome
posted by: winphonegeek on 9/2/2011 10:39:08 PM
We will post the "Windows Phone Mango Local Database(SQL CE): How to Insert data " in a week or so.
Quick question
posted by: rash on 9/16/2011 11:47:01 AM
I'm working on a Silverlight 7.1 app that uses MVVM. I have a DataContext with two tables. One is a Jobs table of sorts listing jobs for an employee to do. And the other is a EmployeeSettings/login table. It gets the settings through a webservice (and gets details such as the employee's name).
Now the settings table will always only have one record. As such it is not an observable collection. However I notice that when I call ....
public EmployeeSettings GetEmployeeSettings()
{
var allEmpSettings = from EmployeeSettings es in serviceDutyDB.settingsTable
select es;
return allEmpSettings.First
And then modify the Name on the returned EmployeeSettings it appears to modify the value in the database, directly. Is this expected behaviour? Does this mean an update is as simple as fetching an object and directly modifying its properties?
I do not seem to even need to call SubmitChanges. (However I have two separate insert and delete methods which do call submitchanges()).
Information on Wp7.1 SQLCE seems sparse and I have not used this approach to DB's before. Most of my background is in ASM so I have an innate distrust for things that happen behind the scenes. I just want to make sure that this is the standard behaviour. (Seeing as you have not yet uploaded your last three tutorials for insert/update/delete).
Cheers
posted by: Andy on 10/8/2011 2:59:47 PM
also looking forward to the rest of the series...
RE: Quick question
posted by: winphonegeek on 10/9/2011 1:13:06 PM
This does not sound right. The expected behavior is that whenever you want to persist something in the database you (or someone else) will have to call SubmitChanges.
Are you sure that what you are seeing is not the result of caching or using the same context instance? Do the changes persist after restarting the application?
Will it be completed?
posted by: Paul Pitchford on 1/4/2012 6:53:47 PM
Hi,
I've been reading you series and wondered when the series may be completed?
Thanks for what you've produced already though, it is excellent.
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
