Windows Phone Mango Local Database(SQL CE): [Table] attribute
published on: 7/19/2011 | Views: N/A | Tags: Mango LocalDB
by WindowsPhoneGeek
This is the third 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 using the [Table] attribute when working with Local Database (SQL CE) in Windows Phone 7.1 Mango.
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
To begin with, lets first mention that basically the local database functionality in Windows Phone 7.1 is an implementation of SQL Compact for Mango. You access the data stored in a local database using LINQ to SQL.
What is [Table] attribute?
Basically you can use this attribute to designate a class as an entity class that is associated with a database table or view. LINQ to SQL treats classes that have this attribute as persistent classes.
You can use the Name property of the TableAttribute attribute to specify a name for the table, and you can optionally use the schema name to qualify a table name. If you do not specify a name by using the Name property, the table name is assumed to be the same as the class name.
In addition to associating classes to tables you will need to denote each field or property you intend to associate with a database column.
NOTE: LINQ to SQL supports only single-table mapping. That is, an entity class must be mapped to exactly one database table, and you cannot map a database table to multiple classes at the same time.
How to use [Table] attribute?
Example1: Table with Name
[Table(Name = "Countries")]
public class Country
{
//class contents here
}
Example2: Table without Name
[Table]
public class Country
{
//class contents here
}
In this article I talked about using [Table] attribute when working with Windows Phone Mango Local Database(SQL CE). Stay tuned for the rest of the posts.
You can also follow us on Twitter @winphonegeek
Comments
Is there a way to dynamically create tables without specifying the TABLE attribute as above
posted by: abk883 on 2/6/2012 9:50:00 AM
Hi
Is there a way to dynamically create tables without specifying the TABLE attribute as above. Specifically, If i do not want to create a Class for the Table specifying all the columns before hand, is there a way to do so.
I saw that "ExecuteCommand" exists in LINQ but Windows Phone doesnot support the same. Any alternates for this ?
Thanks abk
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
