258
Chapter 13: More about Entity Classes
To see how this works, fire up Visual Studio, create a new Windows project, and add the appropriate
references:
system.data.Linq
Next, view the code behind Form1 and add the following statements:
using System.Data.Linq
using System.Data.Linq.Mapping
Prior to Beta2, attribute-based mapping was supported via the System.Data.Linq namespace. If you
then installed Beta2 and tried to compile your code, you received a lot of compile errors. That is because
attribute-based mapping is now supported via the System.Data.Linq.Mapping namespace.
Next, underneath the partial class of Form1, add the following highlighted code:
namespace LINQ
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
public class AdventureWorks : DataContext
{
public AdventureWorks(string connection) : base(connection) {}
public Table
Contacts;
}
[Table(Name = "Person.Contact")]
public class Contact
{
[Column(DbType = "int not null", IsPrimaryKey = true, IsDbGenerated = true)]
public int ContactID;
[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;
259
Part III: LINQ to SQL
[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;
}
The rest of this code should look familiar??”you have seen it throughout the last few chapters.
Pages:
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418