Condividi tramite


How to: Create a LINQ to Entities Project in Visual Studio

The different types of Language-Integrated Query (LINQ) projects require certain imported namespaces (Visual Basic) or using directives (C#) and references. The minimum requirement is a reference to System.Core.dll and System.Data.Entity.dll and a using directive for the System.Linq and System.Data.Objects namespaces. LINQ to Entities also requires a reference to the Entity Data Model (EDM) that will be queried over. The examples in the LINQ to Entities section use the AdventureWorks Sales Model (EDM).

If you are upgrading a project from an earlier version of Visual Studio, you might have to supply these LINQ-related references manually. You might also have to manually set the project to target the .NET Framework version 3.5.

Note

If you are building from a command prompt, you must manually reference the LINQ-related DLLs in drive:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5.

To target the .NET Framework 3.5

  1. In Visual Studio 2008, create a new Visual Basic or C# project. Alternatively, you can open a Visual Basic or C# project that was created in Visual Studio 2005 and convert it to a Visual Studio 2008 project.

  2. On the Project menu, click Properties.

    • For a C# project, in the Application property page, select .NET Framework 3.5 in the Target Framework box.

    • For a Visual Basic project, in the Compile property page, click Advanced Compile Options and then select .NET Framework 3.5 in the Target Framework (all configurations) box.

  3. On the Project menu, click Add Reference.

  4. In the Add Reference dialog box, click the .NET tab, select System.Core from the Component Name list, and then click OK.

  5. For a C# project, add a using directive for the System.Linq and System.Data.Objects namespaces to your source code file or project.

    For a Visual Basic project, add an Imports directive for the System.Linq and System.Data.Objects namespaces to your source code file or project.

To create and reference the AdventureWorks Sales Model

  1. On the Project menu, click Add new item.

  2. In the Templates pane, select ADO.NET Entity Data Model.

  3. Type AdvWorksModel.edmx in the Name box, and then click Add.

  4. Select Generate from database, and then click Next.

  5. In the Choose Your Data Connection window, either select an existing AdventureWorks connection from the list, or create a new connection to a SQL Server instance that has the AdventureWorks sample database.

  6. Save the entity connection settings in App.Config as AdventureWorksEntities and click Next.

  7. In the Choose Your Database Objects dialog box, clear all objects, expand Tables, and select the following table objects:

    • Address (Person)

    • Contact (Person)

    • Product (Production)

    • SalesOrderDetail (Sales)

    • SalesOrderHeader (Sales)

  8. Name the model namespace AdventureWorksModel, and then click Finish.

  9. For a C# project, add a using statement for AdventureWorksEntities to your source file.

    For a Visual Basic project, add an Imports statement for AdventureWorksEntities to your source file.

See Also

Concepts

LINQ to Entities Overview
LINQ to Entities Examples