LINQ to SQL is the family of LINQ technologies targeting relational databases where data is mapped as objects. LINQ to SQL provides the fastest way to create an interface to the backend. The only disadvantage is it can only map one level of relationship. So, if you need deeper relationship management, the Entity Framework is the way to go. Some of the features of LINQ to SQL are as follows:
In this recipe, let's build the MyTask
service like the preceding recipe, but instead of using the Entity Framework we will use LINQ to SQL classes.
Open the Visual Studio and create a new project using the WCF Service Application template and name it Recipe4_LINQToSQL
under the CH7
folder. Right-click on App_Data and select Add new item. Navigate to the preceding recipe's App_Data folder and select the MyTask.mdf
file. We will use the same database and the MyTask
table.
For this recipe, we will again be creating the service application, but this time we will use LINQ to SQL to create the entity objects.
IService1.cs
and Service1.svc
from the project. MyTaskDataModel
, as shown in the following screenshot. When you click on Add
, you should see the MyTaskDataModel.dbml
file in your project. MyTaskService.svc
. MyTaskService.cs
file and add a get
function to return the MyTask
class:[ServiceContract] public interface IMyTaskService { [OperationContract] MyTask GetTask(int value); }
MyTaskService.svc.cs
file and let's add the details for the GetTask
function. In this method, we shall create a data context object and use the LINQ syntax for getting the task by Id:
public class MyTaskService : IMyTaskService { public MyTask GetTask(int value) { MyTaskDataModelDataContext ctx = new MyTaskDataModelDataContext(); return ctx.MyTasks.Where(c => c.Id == value).Single(); } }
MyTaskService.svc
and set it as start page. Now press F5 to run. You should see the service in the WCF Test Client. Select the GetTask request and type a value of 2 and click on Invoke. You should see the following screenshot: Recipe4_LINQToSQLClient
.LINQ to SQL creates the .dbml
file. This file is the proxy file generated by LINQ with the data context object. This file has the class definition for the table, relationships, and properties.
If you open the file MyTaskDataModel.designer.cs
, then you can see the following data context class. This class connects to the database using a connection string that is saved in the config
file.
public partial class MyTaskDataModelDataContext : System.Data.Linq.DataContext { private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); #region Extensibility Method Definitions partial void OnCreated(); #endregion public MyTaskDataModelDataContext() : base(global::System.Configuration.ConfigurationManager. ConnectionStrings["MyTasksConnectionString"].ConnectionString, mappingSource) { OnCreated(); }
We can also see how all the records in the MyTask
table are returned in the following code:
public System.Data.Linq.Table<MyTask> MyTasks { get { return this.GetTable<MyTask>(); } }
LINQ to SQL also creates the MyTask
class with all the table columns as properties. LINQ to SQL supports many types of attributes such as TableAttribute, ColumnAttribute, AssociationAttribute, FunctionAttribute
, table, and so on. We can see the different values of ColumnAttribute
in this file.