Introduction to LINQ to SQL
Basic Introduction to Mapping Entities using LINQ to SQL
Updated: 03 September 2023
Note that you can follow this entire process or skip to the Shortcut section at the end
Using the Microsoft docs on LINQ to SQL and the sample database setup script
You will also need Visual Studio installed
Create the Database
To get started first create the database based on the Script above by applying it from SQL Server Management Server or another database management tool
Typical Process
The typical process for a LINQ to SQL application development is as follows:
- Create a model class and execute simple queries
- Add additional classes and execute more complex queries
- Add, change, and delete database items
- Use Stored Procedures
1. Model Setup and Single Entity Query
Configure the Project
- In Visual Studio create a new .NET Framework Console App called
LinqToSql
- From the Solution Explorer right click on the Project and select
References > Add Reference
and add a reference toSystem.Data.Linq
from theFramework
section - Add the following
usings
to theProgram.cs
file:
Mapping a Class to Database Table
Now we can create a new class and map it to our database table. Create a file in the Project called Models/Customer.cs
with the following class definition. This states that the Table Name for our Customer
collection is Customer
In the class body we can reference the different columns as well as the private internal variable that will hold the value, this is referenced by the Column Annotation, don’t collapse it to a single property (even if Visual Studio tells you to). We can see the full Customer.cs
file below:
Query Database
We need to create a link to our database using a DataContext
object. The documentation makes use of the connection to the mdf
file, however we’ll use a ConnectionString
instead as this makes more sense in practice
We can create a new DataContext
as follows
Thereafter we can query a table in our db
object by using the db.GetTable
function
Using our customers
object we can make queries against the table with:
And print out the reqults of the query with:
Putting this all together, our Program.cs
file should now contain the following:
2. Query Across Relationships
Update the Data Model
First, create the Models/Order.cs
file with the following content:
Note that in the
Order
model we do not have a getter for theOrderId
as this is generated by the database and cannot be set
Next we should add a reference to the Order
model from the Customer
model so as to make it easier to navigate the relationship. We can do this by dding a reference to the Order
entity in our constructor:
And then adding the Orders
property on the field:
Finally the Customer.cs
file should be as follows:
Query Across Multiple Entities
Now we can query this data from the Program.cs
file with a query like the following:
Create a Strongly Typed Database View
It can be easier to create a strongly typed view of the database by creating a DataContext
object that we define instead of using the GetTable
function to retrieve a specific table
Create a class called NorthwindContext
with the following code containing a definition for Customers
and Orders
:
We can now replace DbContext
definition with NorthwindContext
and the call to GetTable
to just use the Customers
property in the NorthwindContext
class like so:
As well as in our second query:
3. Manipulating Data
To manipulate data we can use some of the functions provided to us by LINQ to SQL
We can update data in our database by followind a few basic steps
- Retreive the entity we want to modify
- Create, Update, or Delete the entity
- Submit changes to the datbase
Retrieve an Entity
We can retrieve an entity by wuerying the database for it
Modify Entity
Next you we can modify the City
and delete an Order
from the customer
Submit Changes
Lastly we can submit the changes to the database
4. Using Stored Procedures
In order to use existing Stored Procedures you can make use of the SQLMetal
tool or using the Object Relational Designer
in Visual Studio
Getting Started with the O/R Designer
First, create a new Linq to SQL Class
file from the Right Click Project > Add New Item
dialog, you can call the file DataClasses.dbml
Next add a link to the Database you are working with from the Server Explorer in Visual Studio and you can drag in the Customers
and Orders
table and the relationship should be visible and this should update the generated DataContext
classes with the information you update in the designer. This will not update the database
The O/R Designer only supports 1:1 mappings
You can modify and update information in this view although I would suggest doing it from the
Creating Methods from Stored Procedures can be done by pulling them in from the Stored Procedures
folder on the DB to the methods
section on the designer view
Shortcut
You don’t need to do all the above manually. From Visual Studio do the following:
- Create the project
- Open the database in the Server Explorer
- Add a LinqToSQL Class file (
.dbml
) - Drag in the tables you would like to work and it will generate the