Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Entity Framework ObjectContext?

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?

like image 986
theateist Avatar asked Aug 31 '11 20:08

theateist


People also ask

What is ObjectContext in Entity Framework?

The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in a conceptual model. An instance of the ObjectContext class encapsulates the following: A connection to the database, in the form of an EntityConnection object.

How do you change the code generation strategy in Entity Framework?

Re-Enable ObjectContext Code Generation Open you model in the EF Designer, right-click on a blank section of the design surface and select Properties. In the Properties window change the Code Generation Strategy from None to Default.

What is role of DbContext in Entity Framework?

DbContext is a combination of the Unit Of Work and Repository patterns.” In simplified way we can say that DbContext is the bridge between Entity Framework and Database. Whatever we are doing in Entity Framework (get data, save data, fetch data or any other opration) is done via DbContext.

Whats DbContext?

DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database. DbContext is the primary class that is responsible for interacting with the database.


1 Answers

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());          
}
like image 171
Bobby Richard Avatar answered Nov 15 '22 04:11

Bobby Richard