When mapping an object to a foreign relational data object, you must specify which column is the foreign
key. You do so by defining a method within the table definition and annotating that method with the
Association attribute. This attribute tells the table that it is a foreign key table and identifies the column
that is the foreign key column. The following code shows how the HumanResources.Employee table is
mapped and the foreign key for the table identified:
[Table(Name = "HumanResources.Employee")]
public class Employee
{
[Column(DBType = "int", IsPrimaryKey=true, IsDBGenerated=true, CanBeNull=false)]
public int EmployeeID;
[Column(DBType = "int", CanBeNull=false)]
public int ContactID;
214
Chapter 10: LINQ to SQL Overview
[Column(DBType = "nvarchar(256) not null")]
public string LoginID;
[Column(DBType = "nvarchar(15) not null")]
public string NationalIDNUmber;
[Column(DBType="int")]
public int ManagerID;
private EntityRef
_Contact;
[Association(Name = "FK_Employee_Contact_ContactID",
Storage = "_Contact", ThisKey = "ContactID", IsForeignKey = true)]
public Employee Emp
{
get { return this.
Pages:
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353