The LINQ to SQL Object Model
In LINQ to SQL, an object model expressed in the programming language of the developer is mapped to the data model of a relational database. Operations on the data are then conducted according to the object model.
In this scenario, you do not issue database commands (for example, INSERT) to the database. Instead, you change values and execute methods within your object model. When you want to query the database or send it changes, LINQ to SQL translates your requests into the correct SQL commands and sends those commands to the database.
The most fundamental elements in the LINQ to SQL object model and their relationship to elements in the relational data model are summarized in the following table:
LINQ to SQL Object Model
Relational Data Model
Stored Procedure or Function
The following descriptions assume that you have a basic knowledge of the relational data model and rules.
In LINQ to SQL, a database table is represented by an entity class. An entity class is like any other class you might create except that you annotate the class by using special information that associates the class with a database table. You make this annotation by adding a custom attribute (TableAttribute) to your class declaration, as in the following example:
Only instances of classes declared as tables (that is, entity classes) can be saved to the database.
For more information, see the Table Attribute section of Attribute-Based Mapping (LINQ to SQL).
In addition to associating classes with tables, you designate fields or properties to represent database columns. For this purpose, LINQ to SQL defines the ColumnAttribute attribute, as in the following example:
Only fields and properties mapped to columns are persisted to or retrieved from the database. Those not declared as columns are considered as transient parts of your application logic.
The ColumnAttribute attribute has a variety of properties that you can use to customize these members that represent columns (for example, designating a member as representing a primary key column). For more information, see the Column Attribute section of Attribute-Based Mapping (LINQ to SQL).
In LINQ to SQL, you represent database associations (such as foreign-key to primary-key relationships) by applying the AssociationAttribute attribute. In the following segment of code, the Order class contains a Customer property that has an AssociationAttribute attribute. This property and its attribute provide the Order class with a relationship to the Customer class.
The following code example shows the Customer property from the Order class.
<Association(Name:="FK_Orders_Customers", Storage:="_Customer", ThisKey:="CustomerID", IsForeignKey:=true)> _ Public Property Customer() As Customer Get Return Me._Customer.Entity End Get Set Dim previousValue As Customer = Me._Customer.Entity If (((previousValue Is value) _ = false) _ OrElse (Me._Customer.HasLoadedOrAssignedValue = false)) Then Me.SendPropertyChanging If ((previousValue Is Nothing) _ = false) Then Me._Customer.Entity = Nothing previousValue.Orders.Remove(Me) End If Me._Customer.Entity = value If ((value Is Nothing) _ = false) Then value.Orders.Add(Me) Me._CustomerID = value.CustomerID Else Me._CustomerID = CType(Nothing, String) End If Me.SendPropertyChanged("Customer") End If End Set End Property
For more information, see the Association Attribute section of Attribute-Based Mapping (LINQ to SQL).
LINQ to SQL supports stored procedures and user-defined functions. In LINQ to SQL, you map these database-defined abstractions to client objects so that you can access them in a strongly typed manner from client code. The method signatures resemble as closely as possible the signatures of the procedures and functions defined in the database. You can use IntelliSense to discover these methods.
A result set that is returned by a call to a mapped procedure is a strongly typed collection.
LINQ to SQL maps stored procedures and functions to methods by using the FunctionAttribute and ParameterAttribute attributes. Methods representing stored procedures are distinguished from those representing user-defined functions by the IsComposable property. If this property is set to false (the default), the method represents a stored procedure. If it is set to true, the method represents a database function.
If you are using Visual Studio, you can use the Object Relational Designer to create methods mapped to stored procedures and user-defined functions. How to: Create DataContext Methods Mapped to Stored Procedures and Functions (O/R Designer)
' This is an example of a stored procedure in the Northwind ' sample database. The IsComposable property defaults to false. <FunctionAttribute(Name:="dbo.CustOrderHist")> _ Public Function CustOrderHist(<Parameter(Name:="CustomerID", DbType:="NChar(5)")> ByVal customerID As String) As ISingleResult(Of CustOrderHistResult) Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), customerID) Return CType(result.ReturnValue, ISingleResult(Of CustOrderHistResult)) End Function