方法: EntityConnection の接続文字列を作成するHow to: Build an EntityConnection Connection String

このトピックでは、EntityConnection を作成する方法について説明します。This topic provides an example of how to build an EntityConnection.

この例のコードを実行するにはTo run the code in this example

  1. AdventureWorks Sales Model をプロジェクトに追加し、Entity Framework が使用されるようにプロジェクトを構成します。Add the AdventureWorks Sales Model to your project and configure your project to use the Entity Framework. 詳細については、Entity Data Model ウィザードを使用する」を参照してください。For more information, see How to: Use the Entity Data Model Wizard.

  2. アプリケーションのコード ページで、次の using ステートメント (Visual Basic の場合は Imports) を追加します。In the code page for your application, add the following using statements (Imports in Visual Basic):

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Common;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data.EntityClient;
    using System.Data.Metadata.Edm;
    
    Imports System.Collections.Generic
    Imports System.Collections
    Imports System.Data.Common
    Imports System.Data
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Data.EntityClient
    Imports System.Data.Metadata.Edm
    
    

Example

次の例では、基になるプロバイダーの System.Data.SqlClient.SqlConnectionStringBuilder を初期化した後、System.Data.EntityClient.EntityConnectionStringBuilder オブジェクトを初期化して、このオブジェクトを EntityConnection のコンストラクターに渡しています。The following example initializes the System.Data.SqlClient.SqlConnectionStringBuilder for the underlying provider, then initializes the System.Data.EntityClient.EntityConnectionStringBuilder object and passes this object to the constructor of the EntityConnection.


// Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
string serverName = ".";
string databaseName = "AdventureWorks";

// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder =
    new SqlConnectionStringBuilder();

// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;

// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();

// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
    new EntityConnectionStringBuilder();

//Set the provider name.
entityBuilder.Provider = providerName;

// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;

// Set the Metadata location.
entityBuilder.Metadata = @"res://*/AdventureWorksModel.csdl|
                            res://*/AdventureWorksModel.ssdl|
                            res://*/AdventureWorksModel.msl";
Console.WriteLine(entityBuilder.ToString());

using (EntityConnection conn =
    new EntityConnection(entityBuilder.ToString()))
{
    conn.Open();
    Console.WriteLine("Just testing the connection.");
    conn.Close();
}

' Specify the provider name, server and database. 
Dim providerName As String = "System.Data.SqlClient"
Dim serverName As String = "."
Dim databaseName As String = "AdventureWorks"

' Initialize the connection string builder for the 
' underlying provider. 
Dim sqlBuilder As New SqlConnectionStringBuilder()

' Set the properties for the data source. 
sqlBuilder.DataSource = serverName
sqlBuilder.InitialCatalog = databaseName
sqlBuilder.IntegratedSecurity = True

' Build the SqlConnection connection string. 
Dim providerString As String = sqlBuilder.ToString()

' Initialize the EntityConnectionStringBuilder. 
Dim entityBuilder As New EntityConnectionStringBuilder()

'Set the provider name. 
entityBuilder.Provider = providerName

' Set the provider-specific connection string. 
entityBuilder.ProviderConnectionString = providerString

' Set the Metadata location. 
entityBuilder.Metadata = "res://*/AdventureWorksModel.csdl|res://*/AdventureWorksModel.ssdl|res://*/AdventureWorksModel.msl"
Console.WriteLine(entityBuilder.ToString())

Using conn As New EntityConnection(entityBuilder.ToString())
    conn.Open()
    Console.WriteLine("Just testing the connection.")
    conn.Close()
End Using

関連項目See also