<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>EntityFramework - on code</title>
        <description></description>
        <link>/Tags/EntityFramework/RSS</link>
        <language>en</language>
        <image>
            <url>http://suhair.in/Content/icons/flame.png</url>
            <title>on code</title>
            <link>/Tags/EntityFramework/RSS</link>
            <width>64</width>
            <height>64</height>
        </image>
        <item>
            <dc:creator>Admin</dc:creator>
            <title>EntityConnection code walkthrough</title>
            <description>&lt;pre class=&quot;prettyprint&quot;&gt;// Specify the provider name, server and database.
string providerName = &amp;quot;System.Data.SqlClient&amp;quot;;
string serverName = &amp;quot;.&amp;quot;;
string databaseName = &amp;quot;AdventureWorks&amp;quot;;

// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder =
    new SqlConnectionStringBuilder(); &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;

// 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();&lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;

&lt;/pre&gt;
&lt;p&gt;
	SqlConnectionStringBuilder class &lt;span class=&quot;box&quot;&gt;1&lt;/span&gt; is used to create the Sql Server specific connection string &lt;span class=&quot;box&quot;&gt;2&lt;/span&gt;. Later this generated connection string will be passed to the EntityConnectionStringBuilder instance. This gives the following advantages&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
		Strongly typed connection string properties&lt;/li&gt;
	&lt;li&gt;
		Support for synonyms&lt;/li&gt;
	&lt;li&gt;
		Security&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
    new EntityConnectionStringBuilder();

//Set the provider name.
entityBuilder.Provider = providerName;&lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;

// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;

// Set the Metadata location.
entityBuilder.Metadata = @&amp;quot;res://*/AdventureWorksModel.csdl|
                            res://*/AdventureWorksModel.ssdl|
                            res://*/AdventureWorksModel.msl&amp;quot;;&lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt;
Console.WriteLine(entityBuilder.ToString());


&lt;/pre&gt;
&lt;p&gt;
	Unlike the SqlConnectionStringBuilder, EntityConnectionStringBuilder is vendor agnostic and hence we need to specify the provider name &lt;span class=&quot;box&quot;&gt;3&lt;/span&gt; before generating the Entity connection string. Metadata file locations &lt;span class=&quot;box&quot;&gt;4&lt;/span&gt; should also be specified before constructing the connection string.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;using (EntityConnection conn =
    new EntityConnection(entityBuilder.ToString()))
{
    conn.Open();
    Console.WriteLine(&amp;quot;Just testing the connection.&amp;quot;);
    conn.Close();
}

&lt;!-- pre--&gt;&lt;/pre&gt;</description>
            <link>http://suhair.in/Blog/entity-connection-code-walkthrough</link>
            <guid isPermaLink="true">http://suhair.in/Blog/entity-connection-code-walkthrough</guid>
            <pubDate>Thu, 24 Sep 2009 08:00:00 GMT</pubDate>
            <category>csharp</category>
            <category>EntityFramework</category>
        </item>
    </channel>
</rss>
