LINQ - Tutorial
LINQ to SQL - Generating DataContext

Prerequisites:

Goals:

Steps:

  1. Create a new Console Application project.
  2. Add to the project a new item - 'LINQ to SQL Classes'. Name it 'Nortwind.dbml':
  3. According to the description displayed in the designer of the NorthwindDataClasses.dbml file, open the Server Explorer window and add a new connection:
  4. Expand the new created connection in the Server Explorer window and than expand also the 'Tables' node. Select all tables and drag them to the 'Object Relational Designer' window opened for the NorthwindDataClasses.dbml:
  5. Examine all files generated by Visual Studio:
  6. To check if everything is OK, write to the console the number of customers stored in the Northwind database:
    static void Main(string[] args)
    {
        
    NorthwindDataContext dataContext = new NorthwindDataContext();
        
    Console.WriteLine(dataContext.Customers.Count());
    }

Alternatively, the SqlMetal.exe command line tool can be used for generating the DataContext classes and all classes representing tables.

[Source code]