It is also used together with the
[Association] attribute to define and represent a relationship.
Again, the ThisKey property is used in the definition of the relationship to specify the name of the
property (column) in the related class (table) to which the current class??™s (table??™s) property (column)
is compared.
The next example uses the same two classes??”one for the Contacts table and one for the Employee
table??”as in the previous example. The following code maps these two classes to their respective relational
database counterparts, and also uses the [Association] attribute and the EntityRef type to define
a one-to-many relationship between the two classes. The relationship is defined within the context of the
Employee 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;
248
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;
}
[Table(Name = "HumanResources.
Pages:
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400