How to: Create LINQ to SQL Classes in a Web Project
When you want to use Language-Integrated Query (LINQ) to access data in a database, you do not connect directly to the database. Instead, you create classes that represent the database and its tables, and use those classes to interact with data. You can generate the classes through the Object Relational Designer or by running the SqlMetal.exe utility. For more information, see Object Relational Designer (O/R Designer) and SqlMetal.exe (Code Generation Tool).
This topic shows how to use the O/R Designer in a Web project to create data classes that represent a SQL Server database.
In a Web site project, you must put the data classes in the project's App_Code folder or in a subfolder of App_Code. If you include the data classes in a subfolder of App_Code, the name of the subfolder will be used as the namespace for the classes. In that case, you must provide that namespace when you connect to the data classes.
For a Web application project, you do not have to use the App_Code folder. You can put the data classes in the project folder. For information about the difference between Web site projects and Web application projects, see Web Application Projects versus Web Site Projects.
When you use the O/R Designer, the connection string for accessing the database is automatically added to the Web.config file.
To create a class from a database table
If you are working with a Web site project, and the project does not already have an App_Code folder, in Solution Explorer, right-click the project, click Add ASP.NET Folder, and then click App_Code.
In Solution Explorer, right-click the App_Code folder (for a Web site project) or the project (for a Web application project) and then click Add New Item.
Under Installed templates, select Linq to SQL Classes template, type a name for the .dbml file, and then click Add.
The Object Relational Designer window is displayed.
In Server Explorer, drag the database tables or other objects (such as views) that you want to use with LINQ to SQL into the Object Relational Designer window.
The tables and their columns are represented as a entities in the designer window.
Save the .dbml file.
This creates .designer.cs or .designer.vb file that is located under the .dbml file. The file contains a class that represents the database and a class that represents the table. The parameterless constructor for the database class reads the connection string from the Web.config file.