Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unfortunately, combining multiple entity contexts into a single named connection isn't possible. If you want to use named connection strings from a .config file to define your Entity Framework connections, they will each have to have a different name. By convention, that name is typically the name of the context:</p> <pre><code>&lt;add name="ModEntity" connectionString="metadata=res://*/ModEntity.csdl|res://*/ModEntity.ssdl|res://*/ModEntity.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True&amp;quot;" providerName="System.Data.EntityClient" /&gt; &lt;add name="Entity" connectionString="metadata=res://*/Entity.csdl|res://*/Entity.ssdl|res://*/Entity.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=SOMESERVER;Initial Catalog=SOMECATALOG;Persist Security Info=True;User ID=Entity;Password=Entity;MultipleActiveResultSets=True&amp;quot;" providerName="System.Data.EntityClient" /&gt; </code></pre> <p>However, if you end up with namespace conflicts, you can use any name you want and simply pass the correct name to the context when it is generated:</p> <pre><code>var context = new Entity("EntityV2"); </code></pre> <p>Obviously, this strategy works best if you are using either a factory or dependency injection to produce your contexts.</p> <p>Another option would be to produce each context's entire connection string programmatically, and then pass the whole string in to the constructor (not just the name).</p> <pre><code>// Get "Data Source=SomeServer..." var innerConnectionString = GetInnerConnectionStringFromMachinConfig(); // Build the Entity Framework connection string. var connectionString = CreateEntityConnectionString("Entity", innerConnectionString); var context = new EntityContext(connectionString); </code></pre> <p>How about something like this:</p> <pre><code>Type contextType = typeof(test_Entities); string innerConnectionString = ConfigurationManager.ConnectionStrings["Inner"].ConnectionString; string entConnection = string.Format( "metadata=res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl;provider=System.Data.SqlClient;provider connection string=\"{1}\"", contextType.Name, innerConnectionString); object objContext = Activator.CreateInstance(contextType, entConnection); return objContext as test_Entities; </code></pre> <p>... with the following in your machine.config:</p> <pre><code>&lt;add name="Inner" connectionString="Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /&gt; </code></pre> <p>This way, you can use a single connection string for every context in every project on the machine.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload