A newbie recently wants to learn about the Windows Phone database and find some short tutorials. Because it is in English, it is translated by the way. The English level is not good. It is estimated that there are many mistakes in the text. If you have any children's shoes that are unfortunately read, please keep in doubt about the translation quality and give me more advice.
This is the original address: http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--Association-attribute
The text is as follows:
This is the fifth article in the Windows Phone mango local database (sqlce) series. To get you started using databases in Windows Phone mango, this series of short clips will cover all the things you need to know. I will talk about using [Association] attribute in the Windows Phone mango local database.
First of all, it should be mentioned that the database function on Windows Phone 7.1 is an implementation of mango in SQL compact. You will access the data stored in the database using LINQ to SQL.
1. What is [Association] attribute? associations between entity classes in LINQ to SQL are similar to associations between tables in the database. [Association] attribute is used to specify an attribute in the database to indicate Association. For example, the relationship between a foreign key and a primary key. You can also represent one-to-one or many-to-many relationships.
- One-to-one: Use the entityset <tentity> type that contains the attribute to indicate this relationship at both ends of the association.
- Many-to-many: in the many-to-many relationship, the primary key of the linked list (also called the join table) usually forms a foreign key compound between the other two tables.
Reference: You can see here: http://msdn.microsoft.com/zh-cn/library/system.data.linq.mapping.associationattribute.aspx 2. How to Use the [Association] attribute Association to specify
[Association]Attribute, which allows you to configure the relationship between two types in the database ing. [Association] attribute has the following important attributes:
- The otherkey-attribute name corresponds to the ID of the object on the other end of the Association (obtain or set one or more members of the key value and target object class on the other end of the Association)
- Thiskey-for this type, the attribute name corresponds to the foreign key (this entity class member that gets or sets the key value on this end associated)
- Storage-this attribute supports variables (get or set private storage fields used to save values in the column .)
NoteThe following is an example of one-to-multiple composite association described in msdn. 1:
1 [Table] 2 public class Country 3 { 4 ... 5 6 private EntitySet<City> citiesRef; 7 8 [Association(Name = "FK_Country_Cities", Storage = "citiesRef", ThisKey = "ID", OtherKey = "CountryID")] 9 public EntitySet<City> Cities10 {11 get12 {13 return this.citiesRef;14 }15 }16 ...17 18 }
Note: In the code snippet above, citiesref supports entityset <city> type variables, because citiesref is "one-to-multiple" associated "multiple.
Example 2:
1 [Table] 2 public class City 3 { 4 //... 5 6 private EntityRef<Country> countryRef = new EntityRef<Country>(); 7 8 9 [Association(Name = "FK_Country_Cities", Storage = "countryRef", ThisKey = "CountryID", OtherKey = "ID", IsForeignKey = true)]10 public Country Country11 {12 get13 {14 return this.countryRef.Entity;15 }16 set17 {18 Country previousValue = this.countryRef.Entity;19 if (((previousValue != value) || (this.countryRef.HasLoadedOrAssignedValue == false)))20 {21 if ((previousValue != null))22 {23 this.countryRef.Entity = null;24 previousValue.Cities.Remove(this);25 }26 this.countryRef.Entity = value;27 if ((value != null))28 {29 value.Cities.Add(this);30 this.countryID = value.ID;31 }32 else33 {34 this.countryID = default(Nullable<int>);35 }36 }37 }38 }39 //...40 41 }
Note: In the code snippet above, countryref supports variables of the entityref <country> type, because citiesref is one-to-many associated one-to-one.
You can also refer to here http://www.windowsphonegeek.com/articles/Windows-Phone-Mango-Local-Database-mapping-and-database-operations
This article describes how to use [Association] attribute in a Windows Phone mango local database. Continue to pay attention to the following articles.