共用方式為


HOW TO:定義連接字串 (Entity Framework)

本主題將示範如何定義連接至 實體資料模型 (EDM) 時所使用的連接字串 (Connection String)。本主題是根據 AdventureWorks 銷售 EDM。AdventureWorks Sales Model 會在 Entity Framework 文件的所有工作相關的主題內使用。本主題會假設您已經設定 Entity Framework 並且定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:手動定義模型和對應檔 (Entity Framework)。本主題中的程序也包含在 HOW TO:手動設定 Entity Framework 專案主題中。

Bb896291.note(zh-tw,VS.100).gif注意:
如果您在 Visual Studio 專案中使用 實體資料模型 精靈,它會自動產生 EDM,並設定此專案使用 Entity Framework 。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)

若要定義 EDM 連接字串

  • 開啟專案的應用程式組態檔 (app.config) 並加入下列連接字串:

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

    如果您的專案沒有應用程式組態檔,可以從 [專案] 功能表中選取 [加入新項目]、選取 [一般] 類別、選取 [應用程式組態檔],然後按一下 [加入],藉以加入應用程式組態檔。

另請參閱

概念

快速入門 (Entity Framework)

其他資源

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