The set of entity classes that LINQ to SQL requires is a thin abstraction layer over the relational model. Each entity class defines an accessible table of data, which can be queried and modified. Entity instances that are modified can apply their changes on data contained in the relational database. You will see the options for data updates in the “Data Update” section of this chapter. In this section, you will learn how to build a data model for LINQ to SQL.
DataContext
The DataContext class handles the communication between LINQ and external relational data sources. Each instance has a single Connection to a relational database. Its type is IDbConnection; therefore, it is not specific to a particular database product.
|
Important |
The architecture of LINQ to SQL supports many data providers so that it can map to different underlying relational databases. At the time of this writing, beta versions of LINQ support only Microsoft SQL Server for LINQ to SQL, but anyone can implement providers of his own.
|
DataContext uses metadata information to map the physical structure of relational data, on which the SQL code generation is based. DataContext can also be used to call a stored procedure and persist data changes in entity class instances in the relational database.
Classes that specialize access for a particular database can be derived from DataContext. Such classes offer an easier way to access relational data, including members that represent available tables. You can define the existing tables simply by declaring them, without a specific initialization, as in the following code:
|
Note |
Table members are initialized automatically by the DataContext base constructor, which examines the type at execution time through Reflection, finds those members, and initializes them.
|