This is the fifth 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 using [association] attribute on the Windows Phone Mango Local database.
First and foremost, the database functionality on Windows Phone 7.1 is an implementation of the SQL compact about Mango. You will use LINQ to SQL to access the data stored on the database.
1, [Association] attribute is whatThe association between entity classes in LINQ to SQL is similar to the association between tables in a database. [Association] attribute is used to specify a property in the database to represent the association. For example, a foreign key to a primary key relationship. You can also indicate a one-to-many relationship.
- One-to-one: Use the entityset<tentity> type of the containing property to represent this relationship at both ends of the association.
- Many-to-many: in many-to-many relationships, the primary key of a linked list (also called a junction table) usually forms a foreign key that is compounded by two other tables.
Reference : You can view here: http://msdn.microsoft.com/zh-cn/library/system.data.linq.mapping.associationattribute.aspx2. How to use [Association] attribute associations Specify the use of
[Association] attribute, which allows you to configure relationships between two types in a database map. [Association] attribute has several important properties:
- The name of the Otherkey-property corresponds to the ID of the object at the other end of the association (Gets or sets one or more members of the target entity class as the key value on the other end of the association)
- thiskey-for this type, the name of the property corresponds to the foreign key (Gets or sets the member of this entity class that represents the key value on this side of the association)
- Storage-This property supports variables (Gets or sets the private storage field used to hold the values in the column. )
Note : The explanation in parentheses is MSDN aboveOne -to-many composite associationsExample 1:
1[Table]2PublicClassCountry3{4...56Private entityset<city>Citiesref;78 [Association (Name ="Fk_country_cities", Storage ="Citiesref", ThisKey ="Id", OtherKey ="Countryid ")] 9 public entityset<city> Cities10 {11 get12 {13 return this14 }15 }16 ... 17 18}
Note : In the preceding code fragment, CITIESREF supports variables of type entityset<city> because Citiesref is the "many" side of the "one-to-many" association.
Example 2:
1[Table]2PublicClassCity3{4//...56Private entityref<country> Countryref =New entityref<country>();789 [Association (Name ="Fk_country_cities", Storage ="Countryref", ThisKey ="Countryid", OtherKey ="Id", Isforeignkey =True)]10PublicCountry Country11{12Get13{14ReturnThis. countryref.entity;15}16Set17{Country Previousvalue =This. countryref.entity;19if ((previousvalue! = value) | | (This.countryRef.HasLoadedOrAssignedValue = =False)))20{21stif ((Previousvalue! =Null))22{23This.countryRef.Entity =Null;PreviousValue.Cities.Remove (This);25}26This.countryRef.Entity =Value27if ((Value! =Null))28{Value. Cities.add (This);30This.countryid =Value.id;31 }32 else33 {34 this.countryid = default (Nullable<int>); 35 }36 }37 }38 }39 //...40 41}
Note: In the preceding code fragment, COUNTRYREF supports variables of type entityref<country>, because Citiesref is the "one" side of the "pair-to-many" association.
You can also refer to this http://www.windowsphonegeek.com/articles/Windows-Phone-Mango-Local-Database-mapping-and-database-operations
This article I talked about using [Association] attribute in the Windows Phone Mango Local database. Keep your eye on the next article.