LINQ To SQL Overview CSharp and VB
LINQ To SQL Overview CSharp and VB
LINQ to SQL
.NET Language-Integrated Query for Relational Data
Dinesh Kulkarni, Luca Bolognese, Matt Warren, Anders Hejlsberg, Kit George
Microsoft Corporation
February 2007
Applies to:
Microsoft® Visual Studio® Code Name "Orcas"
Microsoft® .Net Framework 3.5
Summary: LINQ to SQL, a component of Visual Studio Code Name "Orcas", provides a run-time
infrastructure for managing relational data as objects without giving up the ability to query. It
does this by translating language-integrated queries into SQL for execution by the database and
then translating the tabular results back into objects you define. Your application is then free to
manipulate the objects while LINQ to SQL stays in the background tracking your changes
automatically.
Table of Contents
LINQ to SQL......................................................................................................1
1. Introduction
Most programs written today manipulate data in one way or another and often this data is stored
in a relational database. Yet there is a huge divide between modern programming languages and
databases in how they represent and manipulate information. This impedance mismatch is visible
in multiple ways. Most notable is that programming languages access information in databases
through APIs that require queries to be specified as text strings. These queries are significant
portions of the program logic yet they are opaque to the language, unable to benefit from
compile-time verification and design-time features like IntelliSense®.
Of course, the differences go far deeper than that. How information is represented, the data
model, is quite different between the two. Modern programming languages define information in
the form of objects. Relational databases use rows. Objects have unique identity, as each instance
is physically different from another. Rows are identified by primary key values. Objects have
references that identify and link instances together. Rows are left intentionally distinct requiring
related rows to be tied together loosely using foreign keys. Objects stand alone, existing as long
as they are still referenced by another object. Rows exist as elements of tables, vanishing as soon
as they are removed.
It is no wonder that applications expected to bridge this gap are difficult to build and maintain. It
would certainly simplify the equation to get rid of one side or the other. Yet relational databases
provide critical infrastructure for long-term storage and query processing, and modern
programming languages are indispensable for agile development and rich computation.
Until now, it has been the job of the application developer to resolve this mismatch in each
application separately. The best solutions so far have been elaborate database abstraction layers
Chapter 0 LINQ to SQL
that ferry the information between the application's domain specific object models and the tabular
representation of the database, reshaping and reformatting the data each way. Yet by obscuring
the true data source these solutions end up throwing away the most compelling feature of
relational databases; the ability for the data to be queried.
LINQ to SQL, a component of Visual Studio Code Name "Orcas", provides a run-time
infrastructure for managing relational data as objects without giving up the ability to query. It
does this by translating language-integrated queries into SQL for execution by the database and
then translating the tabular results back into objects you define. Your application is then free to
manipulate the objects while LINQ to SQL stays in the background tracking your changes
automatically.
• LINQ to SQL is designed to be non-intrusive to your application. It is possible to migrate
current ADO.NET solutions to LINQ to SQL in a piecemeal fashion, sharing the same
connections and transactions, since LINQ to SQL is simply another component in the ADO.NET
family. LINQ to SQL also has extensive support for stored procedures, allowing reuse of the
existing enterprise assets.
• LINQ to SQL applications are easy to get started. Objects linked to relational data can be
defined just like normal objects, only decorated with attributes to identify how properties
correspond to columns. Of course, it is not even necessary to do this by hand. A design-time
tool is provided to automate translating pre-existing relational database schemas into object
definitions for you.
Together, the LINQ to SQL run-time infrastructure and design-time tools significantly reduce the
workload for the database application developer. The following chapters provide an overview of
how LINQ to SQL can be used to perform common database related tasks. It is assumed that the
reader is familiar with Language-Integrated Query and the Standard Query Operators.
LINQ to SQL is language-agnostic. Any language built to provide Language-Integrated Query can
use it to enable access to information stored in relational databases. The samples in this
document are shown in both C# and Visual Basic; LINQ to SQL can be used with the LINQ-
enabled version of the Visual Basic compiler as well.
2. A Quick Tour
The first step in building a LINQ to SQL application is declaring the object classes you will use to
represent your application data. Let's walk through an example.
[Table(Name="Customers")]
public class Customer
{
public string CustomerID;
public string City;
Chapter 0 LINQ to SQL
[Visual Basic]
<Table(Name:="Customers")> _
Public Class Customer
Public CustomerID As String
Public City As String
End Class
The Table attribute has a Name property that you can use to specify the exact name of the
database table. If no Name property is supplied LINQ to SQL will assume the database table has
the same name as the class. Only instances of classes declared as tables will be able to be stored
in the database. Instances of these types of classes are known as entities, and the classes
themselves, entity classes.
In addition to associating classes to tables you will need to denote each field or property you
intend to associate with a database column. For this, LINQ to SQL defines the Column attribute.
[C#]
[Table(Name="Customers")]
public class Customer
{
[Column(IsPrimaryKey=true)]
public string CustomerID;
[Column]
public string City;
}
[Visual Basic]
<Table(Name:="Customers")> _
Public Class Customer
<Column(IsPrimaryKey:=true)> _
Public CustomerID As String
<Column> _
Public City As String
End Class
Chapter 0 LINQ to SQL
The Column attribute has a variety of properties you can use to customize the exact mapping
between your fields and the database's columns. One property of note is the Id property. It tells
LINQ to SQL that the database column is part of the table's primary key.
As with the Table attribute, you only need to supply information in the Column attribute if it
differs from what can be deduced from your field or property declaration. In this example, you
need to tell LINQ to SQL that the CustomerID field is part of the table's primary key yet you don't
have to specify the exact name or type.
Only fields and properties declared as columns will be persisted to or retrieved from the database.
Others will be considered as transient parts of your application logic.
[Visual Basic]
Each database table is represented as a Table collection accessible via the GetTable() method
using its entity class to identify it. It is recommended that you declare a strongly typed
DataContext instead of relying on the basic DataContext class and the GetTable() method. A
strongly typed DataContext declares all Table collections as members of the context.
[C#]
[Visual Basic]
The query for customers from London can then be expressed more simply as:
Chapter 0 LINQ to SQL
[C#]
[Visual Basic]
Next
We will continue to use the strongly typed Northwind class for the remainder of the overview
document.
[Table(Name="Customers")]
public class Customer
Chapter 0 LINQ to SQL
{
[Column(Id=true)]
public string CustomerID;
...
private EntitySet<Order> _Orders;
[Association(Storage="_Orders", OtherKey="CustomerID")]
public EntitySet<Order> Orders {
get { return this._Orders; }
set { this._Orders.Assign(value); }
}
}
[Visual Basic]
<Table(Name:="Customers")> _
Public Class Customer
<Column(Id:=true)> _
Public CustomerID As String
...
Private _Orders As EntitySet(Of Order)
<Association(Storage:="_Orders", OtherKey:="CustomerID")> _
Public Property Orders() As EntitySet(Of Order)
Get
Return Me._Orders
End Get
Set(ByVal value As EntitySet(Of Order))
End Set
End Property
End Class
The Customer class now has a property that declares the relationship between customers and
their orders. The Orders property is of type EntitySet because the relationship is one-to-many.
We use the OtherKey property in the Association attribute to describe how this association is
Chapter 0 LINQ to SQL
done. It specifies the names of the properties in the related class to be compared with this one.
There was also a ThisKey property we did not specify. Normally, we would use it to list the
members on this side of the relationship. However, by omitting it we allow LINQ to SQL to infer
them from the members that make up the primary key.
Notice how this is reversed in the definition for the Order class.
[C#]
[Table(Name="Orders")]
public class Order
{
[Column(Id=true)]
public int OrderID;
[Column]
public string CustomerID;
private EntityRef<Customer> _Customer;
[Association(Storage="_Customer", ThisKey="CustomerID")]
public Customer Customer {
get { return this._Customer.Entity; }
set { this._Customer.Entity = value; }
}
}
[Visual Basic]
<Table(Name:="Orders")> _
Public Class Order
<Column(Id:=true)> _
Public OrderID As String
<Column> _
Public CustomerID As String
Private _Customer As EntityRef(Of Customer)
<Association(Storage:="_Customer", ThisKey:="CustomerID")> _
Public Property Customer() As Customer
Get
Return Me._Customer.Entity
End Get
Set(ByVal value As Customer)
Chapter 0 LINQ to SQL
Me._Customers.Entity = value
End Set
End Property
End Class
The Order class uses the EntityRef type to describe the relationship back to the customer. The
use of the EntityRef class is required to support deferred loading (discussed later). The
Association attribute for the Customer property specifies the ThisKey property, since the non-
inferable members are now on this side of the relationship.
Also take a look at the Storage property. It tells LINQ to SQL which private member is used to
hold the value of the property. This allows LINQ to SQL to bypass your public property accessors
when it stores and retrieves their value. This is essential if you want LINQ to SQL to avoid any
custom business logic written into your accessors. If the storage property is not specified, the
public accessors will be used instead. You may use the Storage property with Column attributes
as well.
Once you start introducing relationships in your entity classes, the amount of code you need to
write grows as you introduce support for notifications and graph consistency. Fortunately, there is
a tool (described later) that can be used to generate all the necessary definitions as partial
classes, allowing you to use a mix of generated code and custom business logic.
For the rest of this document, we assume the tool has been used to generate a complete
Northwind data context and all entity classes.
var q =
from c in db.Customers
from o in c.Orders
where c.City == "London"
select new { c, o };
[Visual Basic]
The above query uses the Orders property to form the cross product between customers and
orders, producing a new sequence of Customer and Order pairs.
It's also possible to do the reverse.
[C#]
var q =
from o in db.Orders
where o.Customer.City == "London"
select new { c = o.Customer, o };
[Visual Basic]
In this example, the orders are queried and the Customer relationship is used to access
information on the associated Customer object.
[Visual Basic]
When SubmitChanges() is called, LINQ to SQL automatically generates and executes SQL
commands in order to transmit the changes back to the database. It is also possible to override
this behavior with custom logic. The custom logic may call a database stored procedure.
3. Queries in Depth
LINQ to SQL provides an implementation of the standard query operators for objects associated
with tables in a relational database. This chapter describes the LINQ to SQL-specific aspects of
queries.
var q =
from c in db.Customers
Chapter 0 LINQ to SQL
[Visual Basic]
The actual type of q in this instance is IQueryable<Customer>. It's not until the application
attempts to enumerate the contents of the query that it actually executes. In this example the
foreach statement causes the execution to occur.
An IQueryable object is similar to an ADO.NET command object. Having one in hand does not
imply that a query was executed. A command object holds onto a string that describes a query.
Likewise, an IQueryable object holds onto a description of a query encoded as a data structure
known as an Expression. A command object has an ExecuteReader() method that causes
execution, returning results as a DataReader. An IQueryable object has a GetEnumerator()
method that causes the execution, returning results as an IEnumerator<Customer>.
Therefore, it follows that if a query is enumerated twice it will be executed twice.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select c;
// Execute first time
foreach (Customer c in q)
Console.WriteLine(c.CompanyName);
// Execute second time
foreach (Customer c in q)
Console.WriteLine(c.CompanyName);
[Visual Basic]
This behavior is known as deferred execution. Just like with an ADO.NET command object it is
possible to hold onto a query and re-execute it.
Of course, application writers often need to be very explicit about where and when a query is
executed. It would be unexpected if an application were to execute a query multiple times simply
because it needed to examine the results more than once. For example, you may want to bind the
results of a query to something like a DataGrid. The control may enumerate the results each time
it paints on the screen.
To avoid executing multiple times convert the results into any number of standard collection
classes. It is easy to convert the results into a List or Array using the Standard Query Operators
ToL i s t (or
) ToAr ray (.)
[C#]
var q =
from c in db.Customers
where c.City == "London"
select c;
// Execute once using ToList() or ToArray()
var list = q.ToList();
foreach (Customer c in list)
Console.WriteLine(c.CompanyName);
foreach (Customer c in list)
Console.WriteLine(c.CompanyName);
[Visual Basic]
One benefit of deferred execution is that queries may be piecewise constructed with execution
only occurring when the construction is complete. You can start out composing a portion of a
query, assigning it to a local variable and then sometime later continue applying more operators
to it.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select c;
if (orderByLocation) {
q =
from c in q
orderby c.Country, c.City
select c;
}
else if (orderByName) {
q =
from c in q
orderby c.ContactName
select c;
}
foreach (Customer c in q)
Console.WriteLine(c.CompanyName);
[Visual Basic]
In this example q starts out as a query for all customers in London. Later on it changes into an
ordered query depending on application state. By deferring execution the query can be
constructed to suit the exact needs of the application without requiring risky string manipulation.
You might be puzzled by this, since why would any application throw data away? As it turns out
this is how LINQ to SQL manages integrity of the local objects and is able to support optimistic
updates. Since the only changes that occur after the object is initially created are those made by
the application, the intent of the application is clear. If changes by an outside party have occurred
in the interim they will be identified at the time SubmitChanges() is called. More of this is
explained in the section of Chapter 4, Simultaneous Changes.
Note that, in the case that the database contains a table without a primary key, LINQ to SQL
allows queries to be submitted over the table, but it doesn't allow updates. This is because the
framework cannot identify which row to update given the lack of a unique key.
Of course, if the object requested by the query is easily identifiable by its primary key as one
already retrieved no query is executed at all. The identity table acts as a cache storing all
previously retrieved objects.
3.3 Relationships
As we saw in the quick tour, references to other objects or collections of other objects in your
class definitions directly correspond to foreign-key relationships in the database. You can use
these relationships when you query by simply using dot notation to access the relationship
properties, navigating from one object to another. These access operations translate to more
complicated joins or correlated sub-queries in the equivalent SQL, allowing you to walk through
your object graph during a query. For example, the following query navigates from orders to
customers as a way to restrict the results to only those orders for customers located in London.
[C#]
var q =
from o in db.Orders
where o.Customer.City == "London"
select o;
[Visual Basic]
If relationship properties did not exist you would have to write them out manually as joins just as
you would do in a SQL query.
[C#]
var q =
from c in db.Customers
join o in db.Orders on c.CustomerID equals o.CustomerID
where c.City == "London"
select o;
Chapter 0 LINQ to SQL
[Visual Basic]
The relationship property allows you to define this particular relationship once enabling the use of
the more convenient dot syntax. However, this is not the reason why relationship properties exist.
They exist because we tend to define our domain specific object models as hierarchies or graphs.
The objects we choose to program against have references to other objects. It's only a happy
coincidence that since object-to-object relationships correspond to foreign-key style relationships
in databases that property access leads to a convenient way to write joins.
Therefore, in that respect, the existence of relationship properties is more important on the
results side of a query than as part of the query itself. Once you have your hands on a particular
customer, its class definition tells you that customers have orders. So when you look into the
Orders property of a particular customer you expect to see the collection populated with all the
customer's orders, since that is in fact the contract you declared by defining the classes this way.
You expect to see the orders there even if you did not particularly ask for orders up front. You
expect your object model to maintain an illusion that it is an in-memory extension of the
database, with related objects immediately available.
LINQ to SQL implements a technique called deferred loading in order to help maintain this illusion.
When you query for an object you actually only retrieve the objects you asked for. The related
objects are not automatically fetched at the same time. However, the fact that the related objects
are not already loaded is not observable since as soon as you attempt to access them a request
goes out to retrieve them.
[C#]
var q =
from o in db.Orders
where o.ShipVia == 3
select o;
foreach (Order o in q) {
if (o.Freight > 200)
SendCustomerNotification(o.Customer);
ProcessOrder(o);
}
[Visual Basic]
Chapter 0 LINQ to SQL
For example, you may want to query for a particular set of orders and then only occasionally send
an email notification to particular customers. You would not necessary need to retrieve all
customer data up front with every order. Deferred loading allows you to defer the cost of
retrieving extra information until you absolutely have to.
Of course, the opposite might also be true. You might have an application that needs to look at
customer and order data at the same time. You know you need both sets of data. You know your
application is going to drill down through each customer's orders as soon as you get them. It
would be unfortunate to fire off individual queries for orders for every customer. What you really
want to happen is to have the order data retrieved together with the customers.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select c;
foreach (Customer c in q) {
foreach (Order o in c.Orders) {
ProcessCustomerOrder(o);
}
}
[Visual Basic]
Next
Certainly, you can always find a way to join customers and orders together in a query by forming
the cross product and retrieving all the relative bits of data as one big projection. But then the
results would not be entities. Entities are objects with identity that you can modify while the
results would be projections that cannot be changed and persisted. Worse, you would be
retrieving a huge amount of redundant data as each customer repeats for each order in the
flattened join output.
What you really need is a way to retrieve a set of related objects at the same time, a delineated
portion of a graph so you would never be retrieving any more or any less than was necessary for
your intended use.
LINQ to SQL allows you to request immediate loading of a region of your object model for just
this reason. It does this by allowing the specification of a DataShape for a DataContext. The
DataShape class is used to instruct the framework about which objects to retrieve when a
particular type is retrieved. This is accomplished by using the LoadWith method as in the
following:
[C#]
[Visual Basic]
In the previous query, all the Orders for all the Customers who live in London are retrieved when
the query is executed, so that successive access to the Orders property on a Customer object
doesn't trigger a database query.
Chapter 0 LINQ to SQL
The DataShape class can also be used to specify sub-queries that are applied to a relationship
navigation. For example, if you want to retrieve just the Orders that have been shipped today,
you can use the AssociateWith method on the DataShape as in the following:
[C#]
[Visual Basic]
In the previous code, the inner foreach statement iterates just over the Orders that have been
shipped today, because just such orders have been retrieved from the database.
It is important to notice two facts about the DataShape class:
Chapter 0 LINQ to SQL
[Visual Basic]
3.4 Joins
Most queries against object models heavily rely on navigating object references in the object
model. However, there are interesting "relationships" between entities that may not be captured
in the object model as references. For example Customer.Orders is a useful relationship based on
foreign key relationships in the Northwind database. However, Suppliers and Customers in the
same City or Country is an ad hoc relationship that is not based on a foreign key relationship and
may not be captured in the object model. Joins provide an additional mechanism to handle such
relationships. LINQ to SQL supports the new join operators introduced in LINQ.
Consider the following problem — find suppliers and customers based in the same city. The
following query returns supplier and customer company names and the common city as a
flattened result. This is the equivalent of the inner equi-join in relational databases:
[C#]
var q =
from s in db.Suppliers
join c in db.Customers on s.City equals c.City
select new {
Supplier = s.CompanyName,
Customer = c.CompanyName,
City = c.City
};
Chapter 0 LINQ to SQL
[Visual Basic]
The above query eliminates suppliers that are not in the same city as some customer. However,
there are times when we don't want to eliminate one of the entities in an ad hoc relationship. The
following query lists all suppliers with groups of customers for each of the suppliers. If a particular
supplier does not have any customer in the same city, the result is an empty collection of
customers corresponding to that supplier. Note that the results are not flat — each supplier has an
associated collection. Effectively, this provides group join — it joins two sequences and groups
elements of the second sequence by the elements of the first sequence.
[C#]
var q =
from s in db.Suppliers
join c in db.Customers on s.City equals c.City into scusts
select new { s, scusts };
[Visual Basic]
Group join can be extended to multiple collections as well. The following query extends the above
query by listing employees that are in the same city as the supplier. Here, the result shows a
supplier with (possibly empty) collections of customers and employees.
[C#]
var q =
Chapter 0 LINQ to SQL
from s in db.Suppliers
join c in db.Customers on s.City equals c.City into scusts
join e in db.Employees on s.City equals e.City into semps
select new { s, scusts, semps };
[Visual Basic]
The results of a group join can also be flattened. The results of flattening the group join between
suppliers and customers are multiple entries for suppliers with multiple customers in their city —
one per customer. Empty collections are replaced with nulls. This is equivalent to a left outer equi-
join in relational databases.
[C#]
var q =
from s in db.Suppliers
join c in db.Customers on s.City equals c.City into sc
from x in sc.DefaultIfEmpty()
select new {
Supplier = s.CompanyName,
Customer = x.CompanyName,
City = x.City
};
[Visual Basic]
The signatures for underlying Join operators are defined in the Standard Query Operators
document. Only equi-joins are supported and the two operands of equa l s must have the same
type.
3.5 Projections
So far, we have only looked at queries for retrieving entities, objects directly associated with
database tables. We need not constrain ourselves to just this. The beauty of a query language is
that you can retrieve information in any form you want. You will not be able to take advantage of
automatic change tracking or identity management when you do so. However you can get just the
data you want.
For example, you may simply need to know the company names of all customers in London. If
this is the case there is no particular reason to retrieve entire customer objects merely to pick out
names. You can project out the names as part of the query.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select c.CompanyName;
[Visual Basic]
var q =
from c in db.Customers
where c.City == "London"
select new { c.CompanyName, c.Phone };
[Visual Basic]
This example uses an anonymous object initializer to create a structure that holds both the
company name and phone number. You may not know what to call the type, but with implicitly
typed local variable declaration in the language you do not necessarily need to.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select new { c.CompanyName, c.Phone };
foreach(var c in q)
Console.WriteLine("{0}, {1}", c.CompanyName, c.Phone);
[Visual Basic]
If you are consuming the data immediately, anonymous types make a good alternative to
explicitly defining classes to hold your query results.
Chapter 0 LINQ to SQL
You can also form cross products of entire objects, though you might rarely have a reason to do
so.
[C#]
var q =
from c in db.Customers
from o in c.Orders
where c.City == "London"
select new { c, o };
[Visual Basic]
var q =
from c in db.Customers
where c.City == "London"
select new {Name = c.ContactName, c.Phone} into x
orderby x.Name
select x;
[Visual Basic]
Be wary of using parameterized constructors at this stage, though. It is technically valid to do so,
yet it is impossible for LINQ to SQL to track how constructor usage affects member state without
understanding the actual code inside the constructor.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select new MyType(c.ContactName, c.Phone) into x
orderby x.Name
select x;
[Visual Basic]
Because LINQ to SQL attempts to translate the query into pure relational SQL locally defined
object types are not available on the server to actually construct. All object construction is
actually postponed until after the data is retrieved back from the database. In place of actual
constructors, the generated SQL uses normal SQL column projection. Since it is not possible for
the query translator to understand what is happening during a constructor call it is unable to
establish a meaning for the Name field of MyType.
Instead, the best practice is to always use object initializers to encode projections.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select new MyType { Name = c.ContactName, HomePhone = c.Phone } into x
orderby x.Name
select x;
[Visual Basic]
The only safe place to use a parameterized constructor is in the final projection of a query.
[C#]
var e =
new XElement("results",
from c in db.Customers
where c.City == "London"
select new XElement("customer",
new XElement("name", c.ContactName),
new XElement("phone", c.Phone)
)
);
[Visual Basic]
Dim x = <results>
<%= From cust In db.Customers _
Where cust.City = "London" _
Select <customer>
<name><%= cust.ContactName %></name>
<phone><%= cust.Phone %></phone>
</customer>
%>
</results>
You can even use elaborate nesting of object constructors if you desire, like this example that
constructs XML directly out of the result of a query. It works as long as it's the last projection of
the query.
Still, even if constructor calls are understood, calls to local methods may not be. If your final
projection requires invocation of local methods it is unlikely that LINQ to SQL will be able to
oblige. Method calls that do not have a known translation into SQL cannot be used as part of the
query. One exception to this rule is method calls that have no arguments dependent on query
Chapter 0 LINQ to SQL
variables. These are not considered part of the translated query and instead are treated as
parameters.
Still elaborate projections (transformations) may require local procedural logic to implement. For
you to use your own local methods in a final projection you will need to project twice. The first
projection extracts all the data values you'll need to reference and the second projection performs
the transformation. In between these two projections is a call to the AsEnumerable() operator
that shifts processing at that point from a LINQ to SQL query into a locally executed one.
[C#]
var q =
from c in db.Customers
where c.City == "London"
select new { c.ContactName, c.Phone };
var q2 =
from c in q.AsEnumerable()
select new MyType {
Name = DoNameProcessing(c.ContactName),
Phone = DoPhoneProcessing(c.Phone)
};
[Visual Basic]
Note that the AsEnumerable() operator, unlike ToList() and ToArray(), does not cause
execution of the query. It is still deferred. The AsEnumerable() operator merely changes the
static typing of the query, turning a IQueryable<T> (IQueryable (ofT) in Visual Basic) into an
IEnumerable<T> (IEnumerable (ofT) in Visual Basic), tricking the compiler into treating the rest
of the query as locally executed.
Chapter 0 LINQ to SQL
[Visual Basic]
Class Queries
public Shared Function(Of Northwind, String, IQueryable(Of Customer)) _
CustomersByCity = CompiledQuery.Compile( _
Function(db As Northwind, city As String) _
From cust In db.Customers Where cust.City = city)
End Class
The Compile method returns a delegate that can be cached and executed afterward several times
by just changing the input parameters. The following code shows an example of this:
[C#]
[Visual Basic]
LINQ to SQL starts tracking your entities the moment they are retrieved from the database,
before you ever lay your hands on them. Indeed the identity management service discussed
earlier has already kicked in as well. Change tracking costs very little in additional overhead until
you actually start making changes.
[C#]
[Visual Basic]
As soon as the CompanyName is assigned in the example above, LINQ to SQL becomes aware of
the change and is able to record it. The original values of all data members are retained by the
change tracking service.
The change tracking service also records all manipulations of relationship properties. You use
relationship properties to establish the links between your entities, even though they may be
linked by key values in the database. There is no need to directly modify the members associated
with the key columns. LINQ to SQL automatically synchronizes them for you before the changes
are submitted.
[C#]
[Visual Basic]
o.Customer = targetCustomer
Next
You can move orders from one customer to another by simply making an assignment to their
Customer property. Since the relationship exists between the customer and the order, you can
change the relationship by modifying either side. You could have just as easily removed them
from the Orders collection of cust2 and added them to the orders collection of cust1, as shown
below.
[C#]
[Visual Basic]
Of course, if you assign a relationship the value null, you are in fact getting rid of the relationship
completely. Assigning a Customer property of an order to null actually removes the order from
the customer's list.
[C#]
[Visual Basic]
Automatic updating of both sides of a relationship is essential for maintaining consistency of your
object graph. Unlike normal objects, relationships between data are often bidirectional. LINQ to
SQL allow you to use properties to represent relationships, however, it does not offer a service to
automatically keep these bidirectional properties in sync. This is a level of service that must be
baked directly into your class definitions. Entity classes generated using the code generation tool
have this capability. In the next chapter we will show you how to do this to your own handwritten
classes.
It is important to note, however, that removing a relationship does not imply that an object has
been deleted from the database. Remember, the lifetime of the underlying data persists in the
database until the row has been deleted from the table. The only way to actually delete an object
is to remove it from its Table collection.
[C#]
[Visual Basic]
Like with all other changes the order has not actually been deleted yet. It just looks that way to
us since it has been removed and detached from the rest of our objects. When the order object
was removed from the Orders table it was marked for deletion by the change tracking service.
The actually deletion from the database will occur when the changes are submitted on a call to
SubmitChanges(). Note that the object itself is never deleted. The runtime manages the lifetime
of object instances, so it sticks around as long as you are still holding a reference to it. However,
after an object has been removed from its Table and changes submitted it is no longer tracked by
the change tracking service.
The only other time an entity is left untracked is when it exists before the DataContext is aware
of it. This happens whenever you create new objects in your code. You are free to use instances
of entity classes in your application without ever retrieving them from a database. Change tacking
and identity management only apply to those objects that the DataContext is aware of. Therefore
neither service is enabled for newly created instances until you add them to the DataContext.
This can occur in one of two ways. You can call the Add() method on the related Table collection
manually.
[C#]
Customer cust =
new Customer {
Chapter 0 LINQ to SQL
CustomerID = "ABCDE",
ContactName = "Frond Smooty",
CompanyTitle = "Eggbert's Eduware",
Phone = "888-925-6000"
};
// Add new customer to Customers table
db.Customers.Add(cust);
[Visual Basic]
Alternatively, you can attach a new instance to an object that the DataContext is already aware
of.
[C#]
[Visual Basic]
The DataContext will discover your new object instances even if they are attached to other new
instances.
Chapter 0 LINQ to SQL
[C#]
[Visual Basic]
Basically, the DataContext will recognize any entity in your object graph that is not currently
tracked as a new instance, whether or not you called the Add() method.
db.ObjectTracking = false;
[Visual Basic]
db.ObjectTracking = False
[Visual Basic]
When you do call SubmitChanges() the DataContext will attempt to translate all your changes
into equivalent SQL commands, inserting, updating or deleting rows in corresponding tables.
These actions can be overridden by your own custom logic if you desire, however the order of
submission is orchestrated by a service of the DataContext known as the change processor.
The first thing that happens when you call SubmitChanges() is that the set of known objects are
examined to determine if new instances have been attached to them. These new instances are
added to the set of tracked objects. Next, all objects with pending changes are ordered into a
sequence of objects based on dependencies between them. Those objects whose changes depend
on other objects are sequenced after their dependencies. Foreign key constraints and uniqueness
constraints in the database play a big part in determining the correct ordering of changes. Then
just before any actual changes are transmitted, a transaction is started to encapsulate the series
of individual commands unless one is already in scope. Finally, one by one the changes to the
objects are translated into SQL commands and sent to the server.
At this point, any errors detected by the database will cause the submission process to abort and
an exception will be raised. All changes to the database will be rolled back as if none of the
submissions ever took place. The DataContext will still have a full recording of all changes so it is
possible to attempt to rectify the problem and resubmit them by calling SubmitChanges() again.
[C#]
[Visual Basic]
...
' try again
db.SubmitChanges()
End Try
When the transaction around the submission completes successfully the DataContext will accept
the changes to the objects by simply forgetting the change tracking information.
try {
db.SubmitChanges();
break;
}
catch (ChangeConflictException e) {
retries++;
}
}
[Visual Basic]
Try
db.SubmitChanges()
Exit Do
If you are making changes on a middle-tier or server, the easiest thing you can do to rectify a
change conflict is to simply start over and try again, recreating the context and reapplying the
changes. Additional options are described in the following section.
4.4 Transactions
A transaction is a service provided by a databases or any other resource manager that can be
used to guarantee that a series of individual actions occur atomically, meaning either they all
Chapter 0 LINQ to SQL
succeed or they all don't. If they don't, then they are also all automatically undone before
anything else is allowed to happen. If no transaction is already in scope, the DataContext will
automatically start a database transaction to guard updates when you call SubmitChanges().
You may choose to control the type of transaction used, its isolation level or what it actually
encompasses by initiating it yourself. The transaction isolation that the DataContext will use is
known as ReadCommitted.
[C#]
if (prod.UnitsInStock > 0)
prod.UnitsInStock--;
[Visual Basic]
The example above initiates a fully serialized transaction by creating a new transaction scope
object. All database commands executed within the scope of the transaction will be guarded by
the transaction.
[C#]
if (prod.UnitsInStock > 0)
prod.UnitsInStock--;
[Visual Basic]
This modified version of the same example uses the ExecuteCommand() method on the
DataContext to execute a stored procedure in the database right before the changes are
submitted. Regardless of what the stored procedure does to the database, we can be certain its
actions are part of the same transaction.
If the transaction completes successfully the DataContext throws out all the accumulated
tracking information and treats the new states of the entities as unchanged. It does not, however,
rollback the changes to your objects if the transaction fails. This allows you the maximum
flexibility in dealing with problems during change submission.
It is also possible to use a local SQL transaction instead of the new TransactionScope. LINQ to
SQL offers this capability to help you integrate LINQ to SQL features into pre-existing ADO.NET
applications. However, if you go this route you will need to be responsible for much more.
[C#}
Chapter 0 LINQ to SQL
if (prod.UnitsInStock > 0)
prod.UnitsInStock--;
db.Transaction = db.Connection.BeginTransaction();
try {
db.SubmitChanges();
db.Transaction.Commit();
}
catch {
db.Transaction.Rollback();
throw;
}
finally {
db.Transaction = null;
}
[Visual Basic]
db.Transaction = db.Connection.BeginTransaction()
Try
db.SubmitChanges()
db.Transaction.Commit()
catch e As Exception
db.Transaction.Rollback()
Throw e
Finally
Chapter 0 LINQ to SQL
db.Transaction = Nothing
End Try
As you can see, using a manually controlled database transaction is a bit more involved. Not only
do you have to start it yourself, you have to tell the DataContext explicitly to use it by assigning
it to the Transaction property. Then you must use a try-catch block to encase your submit logic,
remembering to explicitly tell the transaction to commit and to explicitly tell the DataContext to
accept changes, or to abort the transactions if there is failure at any point. Also, don't forget to
set the Transaction property back to null when you are done.
You can use the stored procedure instead of the normal auto-generated update command by
defining a method on your strongly typed DataContext. Even if the DataContext class is being
auto-generated by the LINQ to SQL code generation tool, you can still specify these methods in a
partial class of your own.
[C#]
[Visual Basic]
...
End Class
The signature of the method and the generic parameter tells the DataContext to uses this
method in place of a generated update statement. The original and current parameters are used
by LINQ to SQL for passing in the original and current copies of the object of the specified type.
The two parameters are available for optimistic concurrency conflict detection. Note that if you
override the default update logic, conflict detection is your responsibility.
The stored procedure UpdateProductStock is invoked using the ExecuteCommand() method of
the DataContext. It returns the number of rows affected and has the following signature:
[C#]
[Visual Basic]
The object array is used for passing parameters required for executing the command .
Similar to the update method, insert and delete methods may be specified. Insert and delete
methods take only one parameter of the entity type to be updated. For example methods to insert
and delete a Product instance can be specified as follows:
[C#]
[Visual Basic]
[Database(Name="Database#5")]
public class Database5 : DataContext {
...
}
[Visual Basic]
<Database(Name:="Database#5")> _
Public Class Database5
Inherits DataContext
...
End Class
Chapter 0 LINQ to SQL
[Table(Name="Customers")]
public class Customer {
...
}
[Visual Basic]
<Table(Name:="Customers")> _
Public Class Customer
...
End Class
A typical entity class will use Column attributes on public properties and store actual values in
private fields.
[C#]
Chapter 0 LINQ to SQL
[Column(Storage="_city", DBType="NVarChar(15)")]
public string City {
get { ... }
set { ... }
}
[Visual Basic]
<Column(Storage:="_city", DBType:="NVarChar(15)")> _
public Property City As String
Get
set
End Property
The DBType is only specified so that the CreateDatabase() method can construct the table with
the most precise type. Otherwise, the knowledge that the underlying column is limited to 15
characters is unused.
Members representing the primary key of a database type will often be associated with auto-
generated values.
[C#]
[Visual Basic]
Chapter 0 LINQ to SQL
<Column(Storage:="_orderId", IsPrimaryKey:=true, _
IsDbGenerated:= true, DBType:="int NOT NULL IDENTITY")> _
public Property OrderId As String
Get
Set
End Property
If you do specify the DBType make sure to include the IDENTITY modifier. LINQ to SQL will not
augment a custom specified DBType. However, if the DBType is left unspecified LINQ to SQL will
infer that the IDENTITY modifier is needed when creating the Database via the CreateDatabase()
method.
Likewise, if the IsVersion property is true, the DBType must specify the correct modifiers to
designate a version number or timestamp column. If no DBType is specified LINQ to SQL will infer
the correct modifiers.
You can control access to a member associated with an auto-generated column, version stamp, or
any column you might want to hide by designating the access level of the member, or even
limiting the accessor itself.
[C#]
[Visual Basic]
<Column(Storage:="_customerId", DBType:="NCHAR(5)")> _
Public Property CustomerID As String
Get
End Property
Chapter 0 LINQ to SQL
The Order's CustomerID property can be made read-only by not defining a set accessor. LINQ to
SQL can still get and set the underlying value through the storage member.
You can also make a member completely inaccessible to the rest of the application by placing a
Column attribute on a private member. This allows the entity class to contain information relevant
to the class's business logic without exposing it in general. Even though private members are part
of the translated data, since they are private you cannot refer to them in a language-integrated
query.
By default, all members are used to perform optimistic concurrency conflict detection. You can
control whether a particular member is used by specifying its UpdateCheck value.
[C#]
[Column(Storage="_city", UpdateCheck=UpdateCheck.WhenChanged)]
public string City {
get { ... }
set { ... }
}
[Visual Basic]
<Column(Storage:="_city", UpdateCheck:=UpdateCheck.WhenChanged)> _
Public Property City As String
Get
Set
End Property
The following table shows the permissible mappings between database types and the
corresponding CLR type. Use this table as a guide when determine which CLR type to use to
represent a particular database column.
bit, tinyint, Byte, Int16, Lossy conversions possible. Values may not
smallint, int, Uint16, Int32, roundtrip
bigint Uint32, Int64,
Uint64
Bit Boolean
Name String The name of the association. This is often the same as the database's
foreign-key constraint name. It is used when CreateDatabase() is
used to create an instance of the database in order to generate the
relevant constraint. It is also used to help distinguish between multiple
relationships in a single entity class referring to the same target entity
class. In this case, relationship properties on sides of the relationship
(if both are defined) must have the same name.
Storage String The name of the underlying storage member. If specified it tells LINQ
to SQL how to bypass the public property accessor for the data
member and interact with the raw value itself. If not specified LINQ to
SQL gets and sets the value using the public accessor. It is
recommended that all association members be properties with
separate storage members identified.
ThisKey String A comma-separated list of names of one or more members of this
entity class that represent the key values on this side of the
association. If not specified, the members are assumed to be the
members that make up the primary key.
OtherKey String A comma-separated list of names of one or more members of the
target entity class that represent the key values on the other side of
the association. If not specified, the members are assumed to be the
members that make up the other entity class's primary key.
IsUnique Boolean True if there a uniqueness constraint on the foreign key, indicating a
true 1:1 relationship. This property is seldom used as 1:1 relationships
are nearly impossible to manage within the database. Mostly entity
models are defined using 1:n relationships even when they are treated
as 1:1 by application developers.
IsForeignKey Boolean True if the target "other" type of the association is the parent of the
source type. With foreign-key to primary-key relationships, the side
holding the foreign-key is the child and the side holding the primary
key is the parent.
DeleteRule String Used to add delete behavior to this association. For example,
"CASCADE" would add "ON DELETE CASCADE" to the FK relationship. If
set to null, no delete behavior is added.
Association properties either represent a single reference to another entity class instance or they
represent a collection of references. Singleton references must be encoded in the entity class
using the EntityRef<T> (EntityRef (OfT) in Visual Basic) value type to store the actual
reference. The EntityRef type is how LINQ to SQL enables deferred loading of references.
[C#]
class Order
{
...
private EntityRef<Customer> _Customer;
Chapter 0 LINQ to SQL
[Association(Name="FK_Orders_Customers", Storage="_Customer",
ThisKey="CustomerID")]
public Customer Customer {
get { return this._Customer.Entity; }
set { this._Customer.Entity = value;
// Additional code to manage changes }
}
}
[Visual Basic]
Class Order
...
Private _customer As EntityRef(Of Customer)
<Association(Name:="FK_Orders_Customers", _
Storage:="_Customer", ThisKey:="CustomerID")> _
public Property Customer() As Customer
Get
Return _customer.Entity
End Get
Set (value As Customer)
_customer.Entity = value
‘ Additional code to manage changes
End Set
End Class
class Customer
Chapter 0 LINQ to SQL
{
...
private EntitySet<Order> _Orders;
[Association(Name="FK_Orders_Customers", Storage="_Orders",
OtherKey="CustomerID")]
public EntitySet<Order> Orders {
get { return this._Orders; }
set { this._Orders.Assign(value); }
}
}
[Visual Basic]
Class Customer
...
Private _Orders As EntitySet(Of Order)
<Association(Name:="FK_Orders_Customers", _
Storage:="_Orders", OtherKey:="CustomerID")> _
public Property Orders() As EntitySet(Of Order)
Get
Return _Orders
End Get
Set (value As EntitySet(Of Order))
_Orders.Assign(value)
End Property
End Class
class Customer
Chapter 0 LINQ to SQL
{
...
[Association(Name="FK_Orders_Customers", Storage="_Orders",
OtherKey="CustomerID")]
public ICollection<Order> Orders {
get { return this._Orders; }
set { this._Orders.Assign(value); }
}
}
[Visual Basic]
Class Customer
...
Private _orders As EntitySet(Of Order)
<Association(Name:="FK_Orders_Customers", _
Storage:="_Orders", OtherKey:="CustomerID")> _
public Property Orders() As ICollection (Of Order)
Get
Return _orders
End Get
Set (value As ICollection (Of Order))
_orders.Assign(value)
End Property
End Class
Make certain to use the Assign() method on the EntitySet if you expose a public setter for the
property. This allows the entity class to keep using the same collection instance since it may
already be tied into the change tracking service.
Chapter 0 LINQ to SQL
Type Type The Inheritance sub-type. This may be any non-abstract type in the
inheritance hierarchy including the root type.
IsDefault Boolean Determines if the inheritance sub-type specified is the default type
constructed when LINQ to SQL finds a discriminator code that is not
defined by the InheritanceMapping attributes. Exactly one of the
InheritanceMapping attributes must be declared with IsDefault as
true.
[Visual Basic]
The EntitySet<T> (EntitySet(OfT) in Visual Basic) type has a constructor that allows you to
supply two delegates to be used as callbacks, the first when an item is added to the collection,
the second when it is removed. As you can see from the example, the code you specify for these
delegates can and should be written to update the reverse relationship property. This is how the
Customer property on an Order instance is automatically changed when an order is added to a
customer's Orders collection.
Implementing the relationship on the other end is not as easy. The EntityRef<T>
(EntityRef(OfT) in Visual Basic) is a value type defined to contain as little additional overhead
from the actual object reference as possible. It has no room for a pair of delegates. Instead the
code managing graph consistency of singleton references should be embedded in the property
accessors themselves.
[C#]
[Association(Name="FK_Orders_Customers", Storage="_Customer",
ThisKey="CustomerID")]
public Customer Customer {
get {
return this._Customer.Entity;
}
set {
Customer v = this._Customer.Entity;
if (v != value) {
if (v != null) {
this._Customer.Entity = null;
v.Orders.Remove(this);
}
this._Customer.Entity = value;
if (value != null) {
value.Orders.Add(this);
}
}
}
}
[Visual Basic]
<Association(Name:="FK_Orders_Customers", _
Storage:="_Customer", ThisKey:="CustomerID")> _
Chapter 0 LINQ to SQL
_customer.Entity = value
if value IsNot Nothing Then
value.Orders.Add(Me)
End If
End If
End Set
End Property
Take a look at the setter. When the Customer property is being changed the order instance is first
removed from the current customer's Orders collection and then only later added to the new
customer's collection. Notice that before the call to Remove() is made the actual entity reference
is set to null. This is done to avoid recursion when the Remove() method is called. Remember,
the EntitySet will use callback delegates to assign this object's Customer property to null. The
same thing happens right before the call to Add(). The actual entity reference is updated to the
new value. This will again curtail any potential recursion and of course accomplish the task of the
setter in the first place.
The definition of a one-to-one relationship is very similar to the definition of a one-to-many
relationship from the side of the singleton reference. Instead of Add() and Remove() being called,
a new object is assigned or a null is assigned to sever the relationship.
Again, it is vital that relationship properties maintain the consistency of the object graph. If the
in-memory object graph is inconsistent with the database data, then a runtime exception is
generated when the SubmitChanges method is called. Consider using the code generation tool to
maintain consistency work for you.
Chapter 0 LINQ to SQL
[Table(Name="Customers")]
public partial class Customer: INotifyPropertyChanging {
[Column(Storage="_CustomerID", IsPrimaryKey=true)]
public string CustomerID {
get {
return this._CustomerID;
}
set {
if ((this._CustomerID != value)) {
this.OnPropertyChanging("CustomerID");
this._CustomerID = value;
Chapter 0 LINQ to SQL
}
}
}
}
[Visual Basic]
<Table(Name:="Customers")> _
Partial Public Class Customer
Inherits INotifyPropertyChanging
Public Event PropertyChanging As PropertyChangingEventHandler _
Implements INotifyPropertyChanging.PropertyChanging
<Column(Storage:="_CustomerID", IsPrimaryKey:=True)>
public Property CustomerID() As String
Get
Return_customerID
End Get
Set (value As Customer)
If _customerID IsNot value Then
OnPropertyChanging(“CustomerID”)
_CustomerID = value
End IF
End Set
End Function
End Class
Chapter 0 LINQ to SQL
To assist in improved change tracking your entity classes must implement the
INotifyPropertyChanging interface. It only requires you to define an event called
PropertyChanging. The change tracking service then registers with your event when your objects
come into its possession. All you are required to do is raise this event immediately before you are
about to change a property's value.
Don't forget to put the same event raising logic in your relationship property setters too. For
EntitySets, raise the events in the delegates you supply.
[C#]
public Customer() {
this._Orders =
new EntitySet<Order>(
delegate(Order entity) {
this.OnPropertyChanging("Orders");
entity.Customer = this;
},
delegate(Order entity) {
this.onPropertyChanging("Orders");
entity.Customer = null;
}
);
}
[Visual Basic]
o.Customer = Nothing
End Sub
5.4 Inheritance
LINQ to SQL supports single-table mapping, whereby an entire inheritance hierarchy is stored in a
single database table. The table contains the flattened union of all possible data columns for the
whole hierarchy and each row has nulls in the columns that are not applicable to the type of the
instance represented by the row. The single-table mapping strategy is the simplest representation
of inheritance and provides good performance characteristics for many different categories of
queries.
5.4.1 Mapping
To implement this mapping using LINQ to SQL, you need to specify the following attributes and
attribute properties on the root class of the inheritance hierarchy:
• The [Table] (<Table> in Visual Basic) attribute.
• An [InheritanceMapping] (<InheritanceMapping> in Visual Basic) attribute for each class in
the hierarchy structure. For non-abstract classes, this attribute must define a Code property (a
value that appears in the database table in the Inheritance Discriminator column to indicate
which class or subclass this row of data belongs to) and a Type property (which specifies
which class or subclass the key value signifies).
• An IsDefault property on a single [InheritanceMapping] (<InheritanceMapping> in Visual
Basic) attribute. This property serves to designate a "fallback" mapping in case the
discriminator value from the database table does not match any of the Code values in the
inheritance mappings.
• An IsDiscriminator property for a [Column] (<Column> in Visual Basic) attribute, to signify
that this is the column that holds the Code value for inheritance mapping.
No special attributes or properties are required on the subclasses. Note especially that subclasses
do not have the [Table] (<Table> in Visual Basic) attribute.
In the following example, data contained in the Car and Truck subclasses are mapped to the
single database table Vehicle. (To simplify the example, the sample code uses fields rather than
properties for column mapping.)
[C#]
[Table]
[InheritanceMapping(Code = "C", Type = typeof(Car))]
[InheritanceMapping(Code = "T", Type = typeof(Truck))]
[InheritanceMapping(Code = "V", Type = typeof(Vehicle),
IsDefault = true)]
public class Vehicle
{
Chapter 0 LINQ to SQL
[Column(IsDiscriminator = true)]
public string Key;
[Column(IsPrimaryKey = true)]
public string VIN;
[Column]
public string MfgPlant;
}
public class Car : Vehicle
{
[Column]
public int TrimCode;
[Column]
public string ModelName;
}
[Visual Basic]
<Table> _
<InheritanceMapping(Code:="C", Type:=Typeof(Car))> _
<InheritanceMapping(Code:="T", Type:=Typeof(Truck))> _
<InheritanceMapping(Code:="V", Type:=Typeof(Vehicle), _
IsDefault:=true)> _
Public Class Vehicle
<Column(IsDiscriminator:=True)> _
Public Key As String
<Column(IsPrimaryKey:=True)> _
Public VIN As String
Chapter 0 LINQ to SQL
<Column> _
Public MfgPlant As String
End Class
Public Class Car
Inherits Vehicle
<Column> _
Public TrimCode As Integer
<Column> _
Public ModelName As String
End Class
When you view the resulting database diagram in Server Explorer, you see that the columns have
all been mapped to a single table, as shown here:
Note that the types of the columns that represent fields in the subtypes have to be nullable or
they need to have a default specified. This is necessary for the insert commands to be successful.
5.4.2 Querying
The following code provides a flavor of how you can use derived types in your queries:
Chapter 0 LINQ to SQL
[C#]
[Visual Basic]
5.4.3 Advanced
You can expand a hierarchy far beyond the simple sample already provided.
Example 1
Here is a much deeper hierarchy and more complex query:
[C#]
[Table]
[InheritanceMapping(Code = "V", Type = typeof(Vehicle), IsDefault = true)]
[InheritanceMapping(Code = "C", Type = typeof(Car))]
[InheritanceMapping(Code = "T", Type = typeof(Truck))]
[InheritanceMapping(Code = "S", Type = typeof(Semi))]
[InheritanceMapping(Code = "D", Type = typeof(DumpTruck))]
public class Truck: Vehicle { ... }
...
// Get all trucks along with a flag indicating industrial application.
db.Vehicles.OfType<Truck>.Select(t =>
new {Truck=t, IsIndustrial=t is Semi || t is DumpTruck }
);
[Visual Basic]
<Table> _
<InheritanceMapping(Code:="V", Type:=Typeof(Vehicle), IsDefault:=True)> _
<InheritanceMapping(Code:="C", Type:=Typeof(Car))> _
<InheritanceMapping(Code:="T", Type:=Typeof(Truck))> _
<InheritanceMapping(Code:="S", Type:=Typeof(Semi))> _
<InheritanceMapping(Code:="D", Type:=Typeof(DumpTruck))> _
Public Class Truck
InheritsVehicle
Public Class Semi
Inherits Truck
Example 2
The following hierarchy includes interfaces:
[C#]
[Table]
Chapter 0 LINQ to SQL
[Visual Basic]
<Table> _
<InheritanceMapping(Code:="V", Type:=TypeOf(Vehicle),
IsDefault:=True) > _
<InheritanceMapping(Code:="C", Type:=TypeOf(Car)) > _
<InheritanceMapping(Code:="T", Type:=TypeOf(Truck)) > _
<InheritanceMapping(Code:="S", Type:=TypeOf(Semi)) > _
<InheritanceMapping(Code:="H", Type:=TypeOf(Helicopter)) > _
Public Class Truck
Inherits Vehicle
Public Class Semi
InheritsTruck, IRentableVehicle
Public Class Helicopter
InheritsVehicle, IRentableVehicle
[Visual Basic]
6. Advanced Topics
6.1 Creating Databases
Since entity classes have attributes describing the structure of the relational database tables and
columns it is possible to use this information to create new instances of your database. You can
call the CreateDatabase() method on the DataContext to have LINQ to SQL construct a new
database instance with a structure defined by your objects. There are many reasons you might
want to do this. You might be building an application that automatically installs itself on a
customer system or a client application that needs a local database to save its offline state. For
these scenarios the CreateDatabase() is ideal, especially if a known data provider like SQL
Server Express 2005 is available.
However, the data attributes may not encode everything about an existing database's structure.
The contents of user-defined functions, stored procedures, triggers, and check constraints are not
represented by the attributes. The CreateDatabase() function will only create a replica of the
database using the information it knows about, which is the structure of the database and the
types of columns in each table. Yet, for a variety of databases this is sufficient.
Here is an example of how you can create a new database named MyDVDs.mdf.
[C#]
[Table(Name="DVDTable")]
public class DVD
{
[Column(Id = true)]
public string Title;
[Column]
public string Rating;
}
Chapter 0 LINQ to SQL
[Visual Basic]
<Table(Name:="DVDTable")> _
Public Class DVD
<Column(Id:=True)> _
public Title As String
<Column> _
Public Rating As String
End Class
The object model can be used for creating a database using SQL Server Express 2005 as follows:
[C#]
[Visual Basic]
Chapter 0 LINQ to SQL
LINQ to SQL also provides an API to drop an existing database prior to creating a new one. The
database creation code above can be modified to first check for an existing version of the
database using DatabaseExists() and then drop it using DeleteDatabase().
[C#]
if (db.DatabaseExists()) {
Console.WriteLine("Deleting old database...");
db.DeleteDatabase();
}
db.CreateDatabase();
[Visual Basic]
If (db.DatabaseExists()) Then
Console.WriteLine("Deleting old database...")
db.DeleteDatabase()
End If
db.CreateDatabase()
After the call to CreateDatabase() the new database exists and is able to accept queries and
commands like SubmitChanges() to add objects to the MDF file.
It is also possible to use CreateDatabase() with a SKU other than SQL Server Express, using
either an MDF file or just a catalog name. It all depends on what you use for your connection
string. The information in the connection string is used to define the database that will exist, not
necessarily one that already exists. LINQ to SQL will fish out the relevant bits of information and
use it to determine what database to create and on what server to create it. Of course, you will
need database admin rights or equivalent on the server to do so.
Chapter 0 LINQ to SQL
var q =
from c in db.Customers
where c.City == "London"
select c;
[Visual Basic]
You can always access the connection used by your DataContext through the Connection
property and close it yourself.
[C#]
db.Connection.Close();
[Visual Basic]
db.Connection.Close()
You can also supply the DataContext with your own database transaction, in case your
application has already initiated one and you desire the DataContext to play along with it.
[C#]
IDbTransaction = con.BeginTransaction();
...
db.Transaction = myTransaction;
db.SubmitChanges();
db.Transaction = null;
[Visual Basic]
db.Transaction = myTransaction
db.SubmitChanges()
db.Transaction = Nothing
Whenever a Transaction is set, the DataContext will use it whenever it issues a query or
executes a command. Don't forget to assign the property back to null when you are done.
However, the preferred method of doing transactions with the .NET Framework is to use the
TransactionScope object. It allows you to make distributed transactions that work across
databases and other memory resident resource managers. The idea is that transaction scopes
Chapter 0 LINQ to SQL
start cheap, only promoting themselves to full on distributed transaction when they actually do
refer to multiple databases or multiple connections within the scope of the transaction.
[C#]
[Visual Basic]
[Visual Basic]
As long as the column names in the tabular results match column properties of your entity class
LINQ to SQL will materialize your objects out of any SQL query.
The ExecuteQuery() method also allows parameters. In the following code, a parameterized
query is executed:
[C#]
[Visual Basic]
The parameters are expressed in the query text using the same curly notation used by
Console.WriteLine() and String.Format(). In fact, String.Format() is actually called on the
query string you provide, substituting the curly braced parameters with generated parameter
names like @p0, @p1 …, @p(n).
In LINQ to SQL, objects that fail to update because of optimistic concurrency conflicts cause an
exception (ChangeConflictException) to be thrown. You can specify whether the exception
should be thrown at the first failure or whether all updates should be attempted with any failures
being accumulated and reported in the exception.
// [C#]
db.SubmitChanges(ConflictMode.FailOnFirstConflict);
db.SubmitChanges(ConflictMode.ContinueOnConflict);
' [Visual Basic]
db.SubmitChanges(ConflictMode.FailOnFirstConflict)
db.SubmitChanges(ConflictMode.ContinueOnConflict)
When thrown, the exception provides access to an ObjectChangeConflict collection. Details are
available for each conflict (mapped to a single failed update attempt), including access to the
MemberConflicts list. Each member conflict maps to a single member in the update that failed
the concurrency check.
In the scenario above, after conflict resolution, the result in the database is as follows:
Col A Col B Col C
KeepChanges Alfred (User1) Mary (User2) Marketing (User1)
Col A: The original value (Alfreds) remains; User1's value (Alfred) is discarded.
Col B: User2's change (Mary) appears.
Col C: User2's change (Service) appears. User1's change (Marketing) is discarded.
After conflicts have been resolved, you can attempt a resubmit. Because this second update
might also fail, consider using a loop for update attempts.
6.3.4 Examples
The following code excerpts show various informational members and techniques at your disposal
for discovering and resolving member conflicts.
Example 1
Chapter 0 LINQ to SQL
In this example, conflicts are resolved "automatically." That is, database values are merged with
the current client values unless the client has also changed that value (KeepChanges). No
inspection or custom handling of individual member conflicts takes place.
[C#]
try {
context.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException e) {
//automerge database values into current for members
//that client has not modified
context.ChangeConflicts.Resolve(RefreshMode.KeepChanges);
}
//submit succeeds on second try
context.SubmitChanges(ConflictMode.FailOnFirstConflict);
[Visual Basic]
Try
context.SubmitChanges(ConflictMode.ContinueOnConflict)
Catch e As ChangeConflictException
' automerge database values into current for members
' that client has not modified
context.ChangeConflicts.Resolve(RefreshMode.KeepChanges)
End Try
' submit succeeds on second try
context.SubmitChanges(ConflictMode.FailOnFirstConflict)
Example 2
In this example, conflicts are resolved again without any custom handling, but this time database
values are NOT merged into current client values.
[C#]
try {
context.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException e) {
foreach (ObjectChangeConflict cc in context.ChangeConflicts) {
Chapter 0 LINQ to SQL
[Visual Basic]
Try
context.SubmitChanges(ConflictMode.ContinueOnConflict)
Catch e As ChangeConflictException
For Each cc As ObjectChangeConflict In context.ChangeConflicts
‘No database values are automerged into current
cc.Resolve(RefreshMode.KeepCurrentValues)
Next
End Try
Example 3
Here again no custom handling takes place, but in this case all client values are updated with the
current database values.
[C#]
try {
context.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException e) {
foreach (ObjectChangeConflict cc in context.ChangeConflicts) {
//No database values are automerged into current
cc.Resolve(RefreshMode.OverwriteCurrentValues);
}
}
[Visual Basic]
Try
context.SubmitChanges(ConflictMode.ContinueOnConflict)
Catch e As ChangeConflictException
Chapter 0 LINQ to SQL
Example 4
This example shows a way of accessing information on an entity in conflict.
[C#]
try {
user1.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException e) {
Console.WriteLine("Optimistic concurrency error");
Console.ReadLine();
foreach (ObjectChangeConflict cc in user1.ChangeConflicts) {
ITable table = cc.Table;
Customers entityInConflict = (Customers)cc.Object;
Console.WriteLine("Table name: {0}", table.Name);
Console.Write("Customer ID: ");
Console.WriteLine(entityInConflict.CustomerID);
}
}
[Visual Basic]
Try
context.SubmitChanges(ConflictMode.ContinueOnConflict)
Catch e As ChangeConflictException
Console.WriteLine("Optimistic concurrency error")
Console.ReadLine()
For Each cc As ObjectChangeConflict In context.ChangeConflicts
Dim table As ITable = cc.Table
Dim entityInConflict As Customers = CType(cc.Object, Customers)
Chapter 0 LINQ to SQL
Example 5
This example adds a loop through the individual members. Here you could provide custom
handling of any member. (Note: Add using System.Reflection; to provide MemberInfo.)
[C#]
try {
user1.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException e) {
Console.WriteLine("Optimistic concurrency error");
Console.ReadLine();
foreach (ObjectChangeConflict cc in user1.ChangeConflicts) {
ITable table = cc.Table;
Customers entityInConflict = (Customers)cc.Object;
Console.WriteLine("Table name: {0}", table.Name);
Console.Write("Customer ID: ");
Console.WriteLine(entityInConflict.CustomerID);
foreach (MemberChangeConflict mc in cc.MemberConflicts) {
object currVal = mc.CurrentValue;
object origVal = mc.OriginalValue;
object databaseVal = mc.DatabaseValue;
MemberInfo mi = mc. Member;
Console.WriteLine("Member: {0}", mi.Name);
Console.WriteLine("current value: {0}", currVal);
Console.WriteLine("original value: {0}", origVal);
Console.WriteLine("database value: {0}", databaseVal);
Console.ReadLine();
}
}
}
Chapter 0 LINQ to SQL
[Visual Basic]
Try
user1.SubmitChanges(ConflictMode.ContinueOnConflict)
Catch e As ChangeConflictException
Console.WriteLine("Optimistic concurrency error")
Console.ReadLine()
For Each cc As ObjectChangeConflict In context.ChangeConflicts
Dim table As ITable = cc.Table
Dim entityInConflict As Customers = CType(cc.Object, Customers)
Console.WriteLine("Table name: {0}", table.Name)
Console.Write("Customer ID: ")
Console.WriteLine(entityInConflict.CustomerID)
For Each mc As MemberChangeConflict In cc.MemberConflicts
Dim currVal As Object = mc.CurrentValue
Dim origVal As Object = mc.OriginalValue
Dim databaseVal As Object = mc.DatabaseValue
Dim mi As MemberInfo = mc.Member
Console.WriteLine("Member: {0}", mi.Name)
Console.WriteLine("current value: {0}", currVal)
Console.WriteLine("original value: {0}", origVal)
Console.WriteLine("database value: {0}", databaseVal)
Console.ReadLine()
Next
Next
End Try
strongly typed collection. LINQ to SQL can automatically generate the mapped methods, but also
supports manual mapping in situations where you choose not to use code generation.
LINQ to SQL maps stored procedures and functions to methods through the use of attributes. The
StoredProcedure, Parameter, and Function attributes all support a Name property, and the
Parameter attribute also supports a DBType property. Here are two examples:
[C#]
[StoredProcedure()]
public IEnumerable<CustOrderHistResult> CustOrderHist(
[Parameter(Name="CustomerID", DBType="NChar(5)")] string customerID) {
return ((IEnumerable<CustOrderHistResult>)(result.ReturnValue));
}
[Function(Name="[dbo].[ConvertTemp]")]
public string ConvertTemp(string string) { ... }
[Visual Basic]
<StoredProcedure()> _
Public Function CustOrderHist( _
<Parameter(Name:="CustomerID", DBType:="NChar(5)")> _
customerID As String) As IEnumerable(Of CustOrderHistResult)
<Function(Name:="[dbo].[ConvertTemp]")> _
Public Function ConvertTemp(str As String) As String
Chapter 0 LINQ to SQL
The following examples show mappings for various kinds of stored procedures.
Example 1
The following stored procedure takes a single input parameter and returns an integer:
[StoredProcedure(Name = "GetCustomerOrderCount")]
public int GetCustomerOrderCount(
[Parameter(Name = "CustomerID")] string customerID) {
IExecuteResult result = this.ExecuteMethodCall(this,
((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
return (int) result.ReturnValue;
}
[Visual Basic]
<StoredProcedure (Name:="GetCustomerOrderCount")> _
public Function GetCustomerOrderCount( _
<Parameter(Name:= "CustomerID")> customerID As String) As Integer
Dim result As IExecuteResult = ExecuteMethodCall(Me, _
CType(MethodInfo.GetCurrentMethod(), MethodInfo), customerID)
return CInt(result.ReturnValue)
End Function
Example 2
When a stored procedure can return multiple result shapes, the return type cannot be strongly
typed to a single projection shape. In the following example, the result shape depends on the
input:
Chapter 0 LINQ to SQL
[StoredProcedure(Name = "VariableResultShapes")]
[ResultType(typeof(Customer))]
[ResultType(typeof(Order))]
public IMultipleResults VariableResultShapes(System.Nullable<int> shape) {
IExecuteResult result = this.ExecuteMethodCallWithMultipleResults(this,
((MethodInfo)(MethodInfo.GetCurrentMethod())), shape);
return (IMultipleResults) result.ReturnValue;
}
[Visual Basic]
<StoredProcedure(Name:= "VariableResultShapes")> _
<ResultType(typeof(Customer))> _
<ResultType(typeof(Order))> _
public VariableResultShapes(shape As Integer?) As IMultipleResults
Dim result As IExecuteResult =
ExecuteMethodCallWithMultipleResults(Me, _
CType(MethodInfo.GetCurrentMethod(), MethodInfo), shape)
return CType(result.ReturnValue, IMultipleResults)
End Function
Console.WriteLine(c.CompanyName);
}
result = db.VariableResultShapes(2);
foreach (Order o in result.GetResult<Order>()) {
Console.WriteLine(o.OrderID);
}
[Visual Basic]
result = db.VariableResultShapes(2);
For Each o As Order In result.GetResult(Of Order)()
Console.WriteLine(o.OrderID)
Next
Here you need to use the GetResult pattern to get an enumerator of the correct type, based on
your knowledge of the stored procedure. LINQ to SQL can generate all possible projection types,
but has no way of knowing in what order they will be returned. The only way you can know which
generated projection types correspond to a mapped method is by using generated code
comments on the methods.
Example 3
Here is the T-SQL of a stored procedure that returns multiple result shapes sequentially:
LINQ to SQL would map this procedure just as in Example 2 above. In this case, however, there
are two sequential resultsets.
[C#]
[StoredProcedure(Name="MultipleResultTypesSequentially")]
[ResultType(typeof(Product))]
[ResultType(typeof(Customer))]
public IMultipleResults MultipleResultTypesSequentially() {
return ((IMultipleResults)(
this.ExecuteMethodCallWithMultipleResults (this,
((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue
)
);
}
[Visual Basic]
<StoredProcedure(Name:="MultipleResultTypesSequentially")> _
<ResultType(typeof(Customer))> _
<ResultType(typeof(Order))> _
public Function MultipleResultTypesSequentially() As IMultipleResults
Return CType( ExecuteMethodCallWithMultipleResults (Me, _
CType(MethodInfo.GetCurrentMethod(), MethodInfo)), _
IMultipleResults).ReturnValue
End Function
[Visual Basic]
Example 4
LINQ to SQL maps out parameters to reference parameters (ref keyword), and for value types
declares the parameter as nullable (for example, int?). The procedure in the following example
takes a single input parameter and returns an out parameter.
[StoredProcedure(Name = "GetCustomerCompanyName")]
Chapter 0 LINQ to SQL
IExecuteResult result =
this.ExecuteMethodCall(this,
((MethodInfo)(MethodInfo.GetCurrentMethod())),
customerID, companyName);
companyName = (string)result.GetParameterValue(1);
return (int)result.ReturnValue;
}
[Visual Basic]
<StoredProcedure(Name:="GetCustomerCompanyName")> _
Public Function GetCustomerCompanyName( _
customerID As String, ByRef companyName As String) As Integer
companyName = CStr(result.GetParameterValue(1))
return CInt(result.ReturnValue)
End Function
In this case, the method does not have an explicit return value, but the default return value is
mapped anyway. For the output parameter, a corresponding output parameter is used as
expected.
You would call the above stored procedure as follows:
[C#]
[Visual Basic]
var q =
from p in db.Products
select
new {
pid = p.ProductID,
unitp = Math.Floor(p.UnitPrice.Value)
};
[Visual Basic]
Here the method call Math.Floor is translated to a call to the system function 'FLOOR'. In the
same way, a call to a function that is mapped to a UDF is translated to a call to the UDF in SQL.
Example 1
Here is a scalar user-defined function (UDF) ReverseCustName(). In SQL Server, the function
might be defined as follows:
RETURNS varchar(100)
AS
BEGIN
DECLARE @custName varchar(100)
-- Impl. left as exercise for the reader
RETURN @custName
END
You can map a client method defined on a schema class to this UDF using the code below. Note
that the body of the method constructs an expression that captures the intent of the method call,
and passes that expression to the DataContext for translation and execution. (This direct
execution happens only if the function is called.)
[C#]
[Function(Name = "[dbo].[ReverseCustName]")]
public string ReverseCustName(string string1) {
IExecuteResult result = this.ExecuteMethodCall(this,
(MethodInfo)(MethodInfo.GetCurrentMethod())), string1);
return (string) result.ReturnValue;
}
[Visual Basic]
Function(Name:= "[dbo].[ReverseCustName]")> _
Public Function ReverseCustName(string1 As String) As String
Example 2
In the following query, you can see an inline call to the generated UDF method ReverseCustName.
In this case the function is not executed immediately. The SQL built for this query translates to a
call to the UDF defined in the database (see the SQL code following the query).
[C#]
var q =
from c in db.Customers
Chapter 0 LINQ to SQL
select
new {
c.ContactName,
Title = db.ReverseCustName(c.ContactTitle)
};
[Visual Basic]
SELECT [t0].[ContactName],
dbo.ReverseCustName([t0].[ContactTitle]) AS [Title]
FROM [Customers] AS [t0]
When you call the same function outside a query, LINQ to SQL creates a simple query from the
method call expression with the following SQL syntax (where the parameter @p0 is bound to the
constant passed in):
In LINQ to SQL:
[C#]
[Visual Basic]
Converts to:
SELECT dbo.ReverseCustName(@p0)
Example 3
A table-valued function (TVF) returns a single result set (unlike stored procedures, which can
return multiple result shapes). Because the TVF return type is table, you can use a TVF anywhere
in SQL that you can use a table, and you can treat the TVF in the same way as you would a table.
Chapter 0 LINQ to SQL
This function explicitly states that it returns a TABLE, so the returned result set structure is
implicitly defined. LINQ to SQL maps the function as follows:
[C#]
[Function(Name = "[dbo].[ProductsCostingMoreThan]")]
public IQueryable<Product> ProductsCostingMoreThan(
System.Nullable<decimal> cost) {
return this.CreateMethodCallQuery<Product>(this,
(MethodInfo)MethodInfo.GetCurrentMethod(),
cost);
}
[Visual Basic]
<Function(Name:="[dbo].[ProductsCostingMoreThan]")> _
Public Function ProductsCostingMoreThan(
cost As System.Nullable(Of Decimal)) As IQueryable(Of Product)
The following SQL code shows that you can join to the table returned by the function and
otherwise treat it as you would any other table:
FROM dbo.ProductsCostingMoreThan(80.50)
AS p1 INNER JOIN Products AS p2 ON p1.ProductID = p2.ProductID
In LINQ to SQL, the query would be rendered as follows (using the new 'join' syntax):
[C#]
var q =
from p in db.ProductsCostingMoreThan(80.50m)
join s in db.Products on p.ProductID equals s.ProductID
select new {p.ProductID, s.UnitPrice};
[Visual Basic]
database. You might find it necessary to edit the XML file in order for the generator to produce
more pleasing results or to hide aspects of the database that you don't want present in your
objects.
The simplest scenario to use SQLMetal is to directly generate classes from an existing database.
Here is how to invoke the tool:
[C#]
[Visual Basic]
Executing the tool creates a Northwind .cs or .vb file that contains the object model generated
by reading the database metadata. This usage works well if the names of the tables in the
database are similar to the names of the objects that you want to generate. If not you'll want to
take the two-step approach.
To instruct SQLMetal to generate an DBML file use the tool as follows:
Once the dbml file is generated, you can go ahead and annotate it with class and property
attribute to describe how tables and columns map to classes and properties. Once you have
finished annotating the dbml file, you can generate your object model by running the following
command:
[C#]
[Visual Basic]
The following is a table showing the available command line options for SQLMetal.
Option Description
/server:<name> Indicates the server to connect to in order to access the
database.
/database:<name> Indicates the name of the database to read metadata from.
/user:<name> Login user id for the server.
/password:<name> Login password for the server.
/views Extract database views.
/functions Extract database functions.
/sprocs Extract stored procedures.
/code[:<filename>] Indicates that the output of the tool is a source file of entity
class declarations.
/language:<language Use Visual Basic or C# (default).
>
/xml[:<filename>] Indicates that the output of the tools is an DBML file
describing the database metadata and the first guess
approximation of class and property names.
/map[:<filename>] Indicates that an external mapping file should be used
instead of attributes.
/pluralize Indicates that the tool should perform English language
pluralizing / de-pluralizing heuristic to the names of the
tables in order to produce appropriate class and property
names.
/namespace:<name> Indicates the namespace the entity classes will be generated
in.
/timeout:<seconds> Timeout value in seconds to use for database commands.
Note In order to extract the metadata from an MDF file, you must specify the MDF file name
after all other options. If no /server is specified localhost is assumed.
Provider="System.Data.Linq.SqlClient.Sql2005Provider"
xmlns="http://schemas.microsoft.com/dsltools/LINQ to SQLML">
<Table Name="Categories">
<Type Name="Category">
<Column Name="CategoryID" Type="System.Int32"
DbType="Int NOT NULL IDENTITY" IsReadOnly="False"
IsPrimaryKey="True" IsDbGenerated="True" CanBeNull="False" />
<Column Name="CategoryName" Type="System.String"
DbType="NVarChar(15) NOT NULL" CanBeNull="False" />
<Column Name="Description" Type="System.String"
DbType="NText" CanBeNull="True" UpdateCheck="Never" />
<Column Name="Picture" Type="System.Byte[]"
DbType="Image" CanBeNull="True" UpdateCheck="Never" />
<Association Name="FK_Products_Categories" Member="Products"
ThisKey="CategoryID" OtherKey="CategoryID"
OtherTable="Products" DeleteRule="NO ACTION" />
</Type>
</Table>
<Function Name="GetCustomerOrders">
<Parameter Name="customerID" Type="System.String" DbType="NChar(5)" />
<ElementType Name="GetCustomerOrdersResult">
<Column Name="OrderID" Type="System.Int32"
DbType="Int" CanBeNull="True" />
<Column Name="ShipName" Type="System.String"
DbType="NVarChar(40)" CanBeNull="True" />
<Column Name="OrderDate" Type="System.DateTime"
DbType="DateTime" CanBeNull="True" />
<Column Name="Freight" Type="System.Decimal"
DbType="Money" CanBeNull="True" />
</ElementType>
</Function>
</Database>
Database
This is the outermost element in the XML format. This element maps loosely to the Database
attribute on the generated DataContext.
Table
This element represents a database table (or a view) that will be mapped either to a single type
or to an inheritance hierarchy. This element maps loosely to the Table attribute on the generated
entity class.
Type
This element represents a type definition for either a Table or a stored procedure result shape.
This will code-gen into a new CLR type with the columns and associations specified.
Type may also represent a component of an inheritance hierarchy, with multiple types mapping to
the same table. In this case the Type elements are nested to represent the parent-child
inheritance relationships and are differentiated in the database by the InheritanceCode specified.
SubType
This element represents a derived type in an inheritance hierarchy. This will be generated into a
new CLR type with the columns and associations specified in this type. No inheritance attributes
are generated for subtypes.
Comparing to Type, SubType elements do not have AccessModifier because all derived types must
be public. SubTypes cannot be reused by other tables and functions so there is no Id and IdRef in
them.
Column
This element represents a column within a table that is mapped to a property (and backing field)
within a class. There will be no Column element present for either end of a foreign key
relationship, however, as that is completely represented (on both ends) by Association elements.
Association
This element represents either end of a foreign-key relationship. For one-to-many relationships,
this will be an EntitySet<T> on the one side and an EntityRef<T> on the many side. For one-to-
one relationships, this will be an EntityRef<T> on both sides.
Note that it is not required to have an Association entry on both sides of an association. In this
case, a property will only be generated on the side that has the entry (forming a unidirectional
relationship).
Function
This element represents a stored procedure or a database function. For every Function node, a
method is generated in the DataContext class.
TableFunction
This element represents CUD override functions for tables. The LINQ to SQL designer allows
creation of a Insert, Update, and Delete override methods for LINQ TO SQL and allows mapping of
entity property names to stored procedure parameter names.
The method name for CUD functions are fixed so there is no Method attribute in DBML for
TableFunction elements. For example, for the Customer table, the CUD methods are named as
InsertCustomer, UpdateCustomer, and DeleteCustomer.
A table function cannot return tabular shape so there is no ElementType attribute in
TableFunction element.
Chapter 0 LINQ to SQL
Parameter
This element represents a stored procedure/function parameter. Parameters can pass data in and
out.
Return
This element represents the return type of a stored procedure/function.
Chapter 0 LINQ to SQL
TableFunctionParameter
This element represents a parameter of a CUD function. Parameters can pass data in and out,
Every parameter is mapped to a Table column that this CUD function belongs to. There is no Type
or DbType attributes in this element because type information can be obtained from the column
that the parameter maps to.
TableFunctionReturn
This element represents a return type of a CUD function. It actually only contains the column
name that is mapped to the result of the CUD function. The type information of the return can be
obtained from the column.
Connection
This element represents default database connection parameters. This allows the creation of a
default constructor for the DataContext type that already knows how to connect to a database.
Chapter 0 LINQ to SQL
There are two types of default connections possible, one with a direct ConnectionString, and one
that reads from App.Settings.
...
// Tell LINQ to SQL to track this object for an update; that is, not for insertion
Chapter 0 LINQ to SQL
db2.Customers.Attach(C2);
[Visual Basic]
' Customer entity changed on another tier – for example, through a browser
' Back on the mid-tier, a new context needs to be used
Dim db2 As Northwind = New Northwind(…)
...
' Tell LINQ to SQL to track this object for an update; that is, not for insertion
db2.Customers.Attach(C2)
In multi-tier applications, the entire entity is often not sent across tiers for simplicity,
interoperability, or privacy. For example, a supplier may define a data contract for a Web service
that differs from the Order entity used on the middle tier. Likewise, a Web page may show only a
Chapter 0 LINQ to SQL
subset of the members of an Employee entity. Hence, the multi-tier support is designed to
accommodate such cases. Only the members belonging to one or more of the following categories
need to be transported between tiers and set before calling Attach().
1. Members that are part of the entity's identity.
2. Members that have been changed.
3. Members that participate in optimistic concurrency check.
If a timestamp or a version number column is used for optimistic concurrency check, then the
corresponding member must be set before calling Attach(). Values for other members need not
be set before calling Attach(). LINQ to SQL uses minimal updates with optimistic concurrency
checks; that is, a member that is not set or checked for optimistic concurrency is ignored.
Original values required for optimistic concurrency checks may be retained using a variety of
mechanisms outside the scope of LINQ to SQL APIs. An ASP.NET application may use a view state
(or a control that uses the view state). A Web service may use the DataContract for an update
method to ensure that the original values are available for update processing. In the interest of
interoperability and generality, LINQ to SQL does not dictate the shape of the data exchanged
between tiers or the mechanisms used for round-tripping the original values.
Entities for insertion and deletion do not require the Attach() method. The methods used for
two-tier applications — Table.Add() and Table.Remove() can be used for insertion and deletion.
As in case of two-tier updates, a user is responsible for handling foreign key constraints. A
customer with orders cannot be just removed without handling its orders if there is a foreign key
constraint in the database preventing the deletion of a customer with orders.
LINQ to SQL also handles attachment of entities for updates transitively. The user essentially
creates the pre-update object graph as desired and calls Attach(). All changes can then be
"replayed" on the attached graph to accomplish the necessary updates as shown below:
[C#]
c2.Orders.Add(o2);
// Updates
c2.ContactName = ...;
o2.ShipAddress = ...;
// Remove order o1
db2.Orders.Remove(o1);
[Visual Basic]
c2.CustomerID = c.CustomerID
Dim o2 As Order = New Order()
o2.OrderID = ...
c2.Orders.Add(o2)
' Updates
c2.ContactName = ...
o2.ShipAddress = ...
[Visual Basic]
Here is a corresponding snippet from the mapping file showing the mapping for Product class. It
shows the class Product in namespace Mapping mapped to the Products table in Northwind
database. The elements and attributes are consistent with the attribute names and parameters.
<?xml version="1.0" encoding="utf-8"?>
<Database xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="Northwind"
ProviderType="System.Data.Linq.SqlClient.Sql2005Provider">
<Table Name="Products">
<Type Name="Mappings.FunctionMapping.Product">
<Column Name="ProductID" Member="ProductID" Storage="_ProductID"
Chapter 0 LINQ to SQL
6.9.2 System.String
Implemented
Non-static methods:
Length, Substring, Contains, StartsWith, EndsWith, IndexOf, Insert, Remove,
Replace, Trim, ToLower, ToUpper, LastIndexOf, PadRight, PadLeft, Equals, CompareTo.
All signatures are supported, except when they take the StringComparison parameter, and so
on, as detailed below.
Static methods:
Concat(...) all signatures
Compare(String, String)
String (indexer)
Equals(String, String)
Constructor:
String(Char, Int32)
Operators:
+, ==, != (+, =, and <> in Visual Basic)
Not implemented
Chapter 0 LINQ to SQL
Methods that take or produce an array of char, methods that take a CultureInfo /
StringComparison / IFormatProvider
Static (Shared in Visual Basic):
Copy(String str)
Compare(String, String, Boolean)
Compare(String, String, StringComparison)
Compare(String, String, Boolean, CultureInfo)
Compare(String, Int32, String, Int32, Int32)
Compare(String, Int32, String, Int32, Int32, Boolean)
Compare(String, Int32, String, Int32, Int32, StringComparison)
Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
CompareOrdinal(String, String)
CompareOrdinal(String, Int32, String, Int32, Int32)
Join(String, ArrayOf String [,...]) All Join version with first three args
Instance:
ToUpperInvariant()
Format(String, Object) + overloads
IndexOf(String, Int32, StringComparison)
IndexOfAny(ArrayOf Char)
Normalize()
Normalize(NormalizationForm)
IsNormalized()
Split(...)
StartsWith(String, StringComparison)
ToCharArray()
ToUpper(CultureInfo)
TrimEnd(ParamArray Char)
TrimStart(ParamArray Char)
Restrictions / Difference from .NET
SQL uses collations to determine equality and ordering of strings. These can be specified on a SQL
Server Instance, a database, a table column, or an expression.
The translations of the functions implemented so far do not change the collation or specify a
different collation on the translated expressions. So if the default collation is case-insensitive,
functions like CompareTo or IndexOf can give results that differ from what the (case sensitive)
.NET functions would give.
The methods StartsWith(str) / EndsWith(str) assume the argument str is a constant or an
expression that is evaluated on the client. That is, it is currently not possible to use a column for
str.
6.9.3 System.Math
Implemented static methods
Abs, Acos, Asin, Atan, Atan2, BigMul, Ceiling, Cos, Cosh, Exp, Floor, Log, Log10,
Max, Min, Pow, Sign, Sinh, Sqrt, Tan, Tanh, Truncate — all signatures
Not implemented
Chapter 0 LINQ to SQL
IEEERemainder
DivRem has an out parameter, so you cannot use that in an expression. The constants Math.PI
and Math.E are evaluated on the client, so they do not need a translation.
Difference from .NET
The translation of the .NET function Math.Round is the SQL function ROUND. The translation is
supported only when an overload is specified that indicates the MidpointRounding enum value.
The MidpointRounding.AwayFromZero is SQL behavior and MidpointRounding.ToEven indicates
CLR behavior.
6.9.4 System.Convert
Implemented
Methods of form To<Type1>(<Type2> x) where Type1, Type2 is one of:
bool, byte, char, DateTime, decimal, double, float, Int16, Int32, Int64, string.
The behavior is the same as a cast:
• For ToString(Double) there is special code to get the full precision.
• For conversion Int32 / Char, LINQ to SQL uses SQL's UNICODE / NCHAR function.
Otherwise the translation is a CONVERT.
Not Implemented
ToSByte, UInt16, 32, 64: These types do not exist in SQL.
To<integer type>(String, Int32)
ToString(..., Int32) any overload ending with an Int32 toBase
IsDBNull(Object)
GetTypeCode(Object)
ChangeType(...)
Versions with the IFormatProvider parameter.
Methods that involve an array (To/FromBase64CharArray, To/FromBase64String).
6.9.5 System.TimeSpan
Implemented
Constructors:
TimeSpan(Long)
TimeSpan (year, month, day)
TimeSpan (year, month, day, hour, minutes, seconds)
TimeSpan (year, month, day, hour, minutes, seconds, milliseconds)
Operators:
Comparison operators: <,==, and so on in C#; <, =, and so on in Visual Basic
Chapter 0 LINQ to SQL
+, -
Static (Shared in Visual Basic) methods:
Compare(t1,t2)
Non-static (Instance) methods / properties:
Ticks, Milliseconds, Seconds, Hours, Days
TotalMilliseconds, TotalSeconds, TotalMinutes, TotalHours, TotalDays,
Equals, CompareTo(TimeSpan)
Add(TimeSpan), Subtract(TimeSpan)
Duration() [= ABS], Negate()
Not implemented
ToString()
TimeSpan FromDay(Double), FromHours, all From Variants
TimeSpan Parse(String)
6.9.6 System.DateTime
Implemented
Constructors:
DateTime(year, month, day)
DateTime(year, month, day, hour, minutes, seconds)
DateTime(year, month, day, hour, minutes, seconds, milliseconds)
Operators:
Comparisons
DateTime – DateTime (gives TimeSpan)
DateTime + TimeSpan (gives DateTime)
DateTime – TimeSpan (gives DateTime)
Static (Shared) methods:
Add(TimeSpan), AddTicks(Long),
AddDays/Hours/Milliseconds/Minutes (Double)
AddMonths/Years(Int32)
Equals
Non-static (Instance) methods / properties:
Day, Month, Year, Hour, Minute, Second, Millisecond, DayOfWeek
CompareTo(DateTime)
TimeOfDay()
Chapter 0 LINQ to SQL
Equals
ToString()
Difference from .NET
SQL's datetime values are rounded to .000, .003 or .007 seconds, so it is less precise than those
of .NET.
The range of SQL's datetime starts at January 1st, 1753.
SQL does not have a built-in type for TimeSpan. It uses different DATEDIFF methods that return
32-bit integers. One is DATEDIFF(DAY,…), which gives the number of days; another is
DATEDIFF(MILLISECOND,…), which gives the number of milliseconds. An error results if the
DateTimes are more than 24 days apart. In contrast, .NET uses 64-bit integers and measures
TimeSpans in ticks.
To get as close as possible to the .NET semantics in SQL, LINQ to SQL translates TimeSpans into
64-bit integers and uses the two DATEDIFF methods mentioned above to calculate the number of
ticks between two dates.
DateTime UtcNow is evaluated on the client when the query is translated (like any expression that
does not involve database data).
Not implemented
IsDaylightSavingTime()
IsLeapYear(Int32)
DaysInMonth(Int32, Int32)
ToBinary()
ToFileTime()
ToFileTimeUtc()
ToLongDateString()
ToLongTimeString()
ToOADate()
ToShortDateString()
ToShortTimeString()
ToUniversalTime()
FromBinary(Long), FileTime, FileTimeUtc, OADate
GetDateTimeFormats(...)
constructor DateTime(Long)
Parse(String)
DayOfYear
Chapter 0 LINQ to SQL
Member Purpose
Log Prints SQL before it is executed. Covers query, insert, update,
delete commands. Usage:
[C#] db.Log = Console.Out;
[Visual Basic] db.Log = Console.Out
GetQueryText(query) Returns the query text of the query without of executing it. Usage:
[C#]
Console.WriteLine(db.GetQueryText(db.Customers));
[Visual Basic]
Console.WriteLine(db.GetQueryTest(db.Customers))
GetChangeText() Returns the text of SQL commands for insert/update/delete
without executing them. Usage:
[C#] Console.WriteLine(db.GetChangeText());
[Visual Basic] Console.WriteLine(db.GetChangeText())