Home > Articles > Programming > ASP .NET

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Creating LINQ to SQL Entities

Before you can start using LINQ to SQL in your ASP.NET application, you must first create your LINQ to SQL entities. A LINQ to SQL entity is a C# or Visual Basic .NET class that represents an entity from your database. For example, if your database contains a table named Products, you’ll create a LINQ to SQL entity named Product that represents each product from the Products database table. The Product class will include a property that corresponds to each column in your database table.

Visual Studio 2008 makes it easy to create LINQ to SQL entities. You create LINQ to SQL entities by using the Visual Studio Object Relational Designer (see Figure 2). To create new entities, simply drag database tables from the Server Explorer/Database Explorer window onto the Object Relational Designer.

Figure 2

Figure 2 Using the Visual Studio Object Relational Designer.

Let’s assume that your database contains the following table named Products:

Column Name

Column Type


Int (identity, primary key)





Follow these steps to create a new LINQ to SQL entity that represents this database table:

  1. From the menu, select Website > Add New Item.
  2. In the Add New Item dialog box, select LINQ to SQL Classes.
  3. In the Name text box, type Store.dbml (see Figure 3).
  4. Click Add.
    Figure 3

    Figure 3 Creating a new entity.

  5. When a warning message appears, suggesting that the LINQ to SQL classes be added to your App_Code folder, succumb to the suggestion and click Yes.
  6. When the Object Relational Designer appears, drag one or more tables onto the Designer surface from the Server Explorer/Database Explorer. After you drag the Products table onto the Designer surface, you’ll have a new entity named Product. (Visual Studio 2008 changes the name from Products to Product automatically.)
  7. The new Product entity includes a property for each of the columns in the underlying database table. You can view information about each entity property by selecting it and looking in the Properties window. For example, Figure 4 shows the values for the Id property.

Notice that Visual Studio has detected that the Id property represents a primary key and identity value automatically. In the property sheet in Figure 4, both the Primary Key property and the Auto Generated Value property have the value True. If Visual Studio ever gets this setting wrong, you can change these properties manually. For example, I add a column to all of my database tables that has a default value of GetDate(). That way, every time I add a new row to the table, the row gets a date and time stamp automatically. However, the Object Relational Designer doesn’t recognize columns with a default value as being auto-generated. Therefore, I always end up changing the Auto Generated Value property manually for these types of columns.

Figure 4

Figure 4 The Id property’s properties.

Behind the scenes, the Object Relational Designer is generating classes that represent the LINQ to SQL entities. You can view these classes by expanding the Store.dbml file and opening the Store.designer.cs or Store.designer.vb file.

  • + Share This
  • 🔖 Save To Your Account