<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="html">EntityFramework - on code</title>
  <icon>http://suhair.in/Content/icons/flame.ico</icon>
  <logo>http://suhair.in/Content/icons/flame.png</logo>
  <updated>2009-09-24T08:00:00</updated>
  <id>http://suhair.in/tags/entityframework/atom</id>
  <link rel="alternate" type="text/html" hreflang="en" href="/tags/entityframework/atom"/>
  <link rel="self" type="application/atom+xml" href="http://suhair.in/Tags/EntityFramework/ATOM"/>
  <generator uri="http://oxite.net" version="1.0">Oxite</generator>
  <entry>
    <title type="html">EntityConnection code walkthrough</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/entity-connection-code-walkthrough"/>
    <id>http://suhair.in/Blog/entity-connection-code-walkthrough</id>
    <updated>2009-09-27T10:57:21.487</updated>
    <published>2009-09-24T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="csharp" />
    <category term="EntityFramework" />
    <content type="html" xml:lang="en">
      &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;
    </content>
  </entry>
</feed>
