5

I have many DBs in one SQL server. I placed connectionString as template(look at Initial Catalog={0}) into web.config.

<add name="ent" connectionString="metadata=res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=1.1.1.1;Initial Catalog={0};Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

I want to create the objectContext with correct connectionString. I thought to do the following, CreatObjectContext<SiteEntities>('MySite') but I get error Unable to determine the provider name for connection of type 'System.Data.EntityClient.EntityConnection'.

public T CreatObjectContext<T>(string dbName) where T : ObjectContext, new()
{          
       var conStr = ConfigurationManager.ConnectionStrings["ent"].ConnectionString;
       var entityBuilder = new EntityConnectionStringBuilder(conStr);
       entityBuilder.Provider = "System.Data.SqlClient";
       // Build correct conString to the db
       entityBuilder.ProviderConnectionString = string.Format(entityBuilder.ProviderConnectionString, dbName);

       var connection = new EntityConnection(entityBuilder.ConnectionString);                          
       var builder = new ContextBuilder<T>();

       return builder.Create(connection);           
}

What I'm doing wrong? How I can create the context?

0

2 Answers 2

4

If you are using EntityConnectionStringBuilder, you only need to store the sqlserver connection strings in your web.config. EntityConnectionStringBuilder can then convert those to EF4 connection strings.

Example web.config

    <connectionStrings>
      <add name="db1" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db1;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" /> 
      <add name="db2" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" />
    </connectionStrings>

And we can change your method to something like:

public ObjectContext CreatObjectContext(string dbName)
{          
       var conStr = ConfigurationManager.ConnectionStrings[dbName].ConnectionString;
       var entityBuilder = new EntityConnectionStringBuilder();

       entityBuilder.Provider = "System.Data.SqlClient";
       entityBuilder.ProviderConnectionString = conStr;

       entityBuilder.MetaData = @"res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl";

       return new ObjectContext(entityBuilder.ToString());          
}
Sign up to request clarification or add additional context in comments.

1 Comment

Related question entity-framework-runtime-connection-string stackoverflow.com/questions/11368897/…
2

I just wanted to share a small class to create an entity framework connection using the entity class as type of T an SQL connection string and the entityModel meta data name.

    public static class EFConnection<T> where T : ObjectContext
    {
    public static T GetDatabase(string connectionString,string entityModelMetadataName)
    {

        var entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = connectionString;
        entityBuilder.Metadata = @"res://*/" + entityModelMetadataName + ".csdl|res://*/" + entityModelMetadataName + ".ssdl|res://*/" + entityModelMetadataName + ".msl";


    var _db=(T)Activator.CreateInstance(typeof(T), new object[] { entityBuilder.ToString()});
    return _db;

    }

}

use example:

    var _db = EFConnection<Model1Container>.GetDatabase(Settings.General.Default.DatabaseConnectionString, "Model1");

I did use this post also to put everything together:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.