共用方式為


How to: Define the Connection String (Entity Framework)

This topic shows how to define the connection string that is used when connecting to an Entity Data Model (EDM). This topic is based on the AdventureWorks Sales EDM. The AdventureWorks Sales Model is used throughout the task-related topics in the Entity Framework documentation. This topic assumes that you have already configured Entity Framework and defined the AdventureWorks Sales Model. For more information, see How to: Manually Define an Entity Data Model (Entity Framework). The procedures in this topic are also included in the topic How to: Manually Configure an Entity Framework Project.

Note

If you use the Entity Data Model Wizard in a Visual Studio project, it automatically generates an EDM and configures the project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework)

To define the EDM connection string

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

    <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" />
    

    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.

See Also

Concepts

Quickstart (Entity Framework)

Other Resources

How to: Create a New Entity Data Model
Entity Data Model Tools