The OtherKey property is also used in the definition of the relationship. It specifies the name of the
property (column) in the related class (table) to which is compared the current class??™s (table??™s) property
(column).
The following example defines two classes, one for the Contacts table and one for the Employee table. In
the AdventureWorks database, you can see that there??™s a relationship defined between these two tables
on the ContactID column. The code here maps these two classes to their respective relational database
counterparts, and also uses the [Association] attribute and the EntitySet type to define a one-to-many
relationship between the Contact class and the Employee class. The relationship is defined within the
context of the Contact class.
public class AdventureWorks : DataContext
{
public AdventureWorks(string connection) : base(connection) {}
public Table
Contacts;
public Table Employees;
}
[Table(Name = "Person.Contact")]
public class Contact
{
[Column(DBType = "int not null", IsPrimaryKey = true, IsDBGenerated
= true)]
public int ContactID;
246
Chapter 12: Advanced Query Concepts
[Column(DBType = "nvarchar(8) not null")]
public string Title;
[Column(DBType = "nvarchar(50) not null")]
public string FirstName;
[Column(DBType = "nvarchar(50) not null")]
public string MiddleName;
[Column(DBType = "nvarchar(50) not null")]
public string LastName;
[Column(DBType = "nvarchar(50) not null")]
public string EmailAddress;
[Column(DBType = "int")]
public int EmailPromotion;
[Column(DBType = "bit")]
public byte NameStyle;
[Column(DBType = "varchar(40)")]
public string PasswordHash;
[Column(DBType = "varchar(40)")]
public string PasswordSalt;
private EntitySet _employees;
[Association(Storage = "_employees", OtherKey = "ContactID")]
public EntitySet Emps
{
get { return this.
Pages:
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398