Now that we have learned all of the new features for C# 3.0, including LINQ and LINQ to SQL, we will use them in the data access layer of a WCF service. We will create a new WCF service, which is very similar to the one we created in the previous chapters, but in this service, we will use LINQ to SQL to connect to the Northwind
database, to retrieve and update a product.
In the data access layer, we will use LINQ to SQL to retrieve product information from the database, and return it to the business logic layer. You will see that with LINQ to SQL, we will need only one LINQ statement in the GetProduct
method, and we will no longer need to worry about the database connection, or the actual query statement.
In this chapter, we will also learn how to update a product with LINQ to SQL in the data access layer. We will see how to attach an entity object to LINQ to SQL DataContext
, and leave all of the update work to LINQ to SQL, and will also see how to control the concurrency of updates with LINQ to SQL.
In this chapter, we will cover:
Northwind
database in LINQ to SQL designerFrom this point on on, in the first few sections of this chapter, we will use Service Factory for creating the solution files, modeling the service, and generating the source code. The steps here are very similar to those discussed in Chapter 7, so we will not have screenshots for every step. You can follow the steps here to quickly create the solution, and refer back to Chapter 7 for detailed instructions if you have any doubts. You can also download the source code for this chapter, if you don't want to repeat all of these steps.
One thing that is different here is that we will add an operation of UpdateProduct
in this chapter, so that we can test the concurrent updates with LINQ to SQL later on.
To start, follow these steps to create the initial solution files: