Compartilhar via


How to: Manually Configure an Entity Framework Project

If you use the Entity Data Model Wizard in a Visual Studio project, the wizard automatically generates an .edmx file and configures the project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard. You can also manually configure a Visual Studio project to use the Entity Framework. Do this if you have manually defined the model and mapping files or defined them by using the EDM Generator (EdmGen.exe) utility. 

The examples in this topic use the model and mapping files for the AdventureWorks Sales model. The AdventureWorks Sales Model is used throughout the task-related topics in the Entity Framework documentation.

To configure a Visual Studio project to use the AdventureWorks Sales Model

  1. In Solution Explorer, add assembly references to System.Data.Entity.dll and System.Runtime.Serialization.dll.

  2. Add the following model and mapping files to the project:

    • AdventureWorks.csdl

    • AdventureWorks.msl

    • AdventureWorks.ssdl

    For information about creating these files, see How to: Manually Define the Model and Mapping Files.

  3. Select the three files you just added to the project directory. On the Project menu, click Include In Project.

  4. Select the three files you added to the project directory. On the Project menu, click Properties.

  5. In the Properties pane, set Copy to Output Directory to Copy if newer.

  6. Open the project's application configuration file (App.config) and add the following connection string:

    <connectionStrings>
        <add name="AdventureWorksEntities" 
             connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
             provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
             Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
             multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />
    </connectionStrings>
    

    If your project does not have an application configuration file, you can add one by selecting Add New Item from the Project menu, selecting the General category, selecting Application Configuration File, and then clicking Add.

  7. At the command prompt in your project directory, run the appropriate command for your project (with line breaks removed):

    • For C#:

      "%windir%\Microsoft.NET\Framework\v4.0\edmgen.exe" /mode:EntityClassGeneration 
      /incsdl:.\AdventureWorks.csdl /outobjectlayer:.\AdventureWorks.Objects.cs /language:CSharp
      
    • For Visual Basic:

      "%windir%\Microsoft.NET\Framework\v4.0\edmgen.exe" /mode:EntityClassGeneration 
      /incsdl:.\AdventureWorks.csdl /outobjectlayer:.\AdventureWorks.Objects.vb /language:VB
      

    This generates an object layer file in either C# or Visual Basic that is based on the conceptual model.

  8. Add the object-layer file generated in the previous step to your project.

  9. In the code page for your application, add the following using statements (Imports in Visual Basic):

    Imports System
    Imports System.Linq
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Data
    Imports System.Data.Common
    Imports System.Data.Objects
    Imports System.Data.Objects.DataClasses
    
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.Common;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    

See Also

Concepts

Quickstart

Other Resources

Entity Data Model Tools