none
How to programmatically create DbContext Connection string and specify providerName in it?

    Question

  • Hi

     

    I am using code first and SQLite as provder.  I have a DbContext derived class MyDBContext and connection string is provided in web.config file as below including the provider. I would like to be able to create the connection string programmatically with differeing datasource and other properties based on the scneario. That means, create conenction string programmatically and create the db context from a factory method(Factory patternis not important here). Entity framework has EntityConnectionStringBuilder class that is to be used for database or mdoel first approach. My quesitons, how to create a connection string programmatically for code first with the provider name so that code first will use the given provider in config file?

     

    Please note that I dont want to handle connection myself and instead would like to have EF create and close whenever needed. So, I am avoiding passing created connection object to MyDbContext class. I want to pass just the connection string which would then be passed to DbContext using base(conenctionString). Constructor as below

     

    public MyDbContext(string connectionString):base(connectionString){}

     

     

    <DbProviderFactories>
      <remove invariant="System.Data.SQLite"/>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite"
       description=".Net Framework Data Provider for SQLite"
       type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.72.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
     </DbProviderFactories>
     </system.data>
     
     <connectionStrings>
     <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
     <add name="MyDBContext"
      connectionString="Data Source=./App_Data/myCards.db;Version=3;New=True;"
      providerName="System.Data.SQLite"
      />
     </connectionStrings>
    

     

    Thanks.

    Suresh

    • Edited by Scribnar Monday, May 16, 2011 12:25 PM Just added signature
    Monday, May 16, 2011 12:24 PM

Answers

  • I solved this by having the connectionString property set to empty as below in config file and then changing my constructor of MyDBContext too as below. It works but I wonder if its in line with good practices. I am not able to find link for guidelines that addresses this issue in entity framework blogs or msdn. Can anyone point me please to the right guidelines?  Thanks.

     

    public MyDBContext(string fileWithPath)
    {
    this.Database.Connection.ConnectionString = GetConnectionString(fileWithPath);
    }
    

     

    <add name="MyDBContext"
        connectionString=""
        providerName="System.Data.SQLite"
       />
    
    Monday, May 16, 2011 1:14 PM

All replies

  • I solved this by having the connectionString property set to empty as below in config file and then changing my constructor of MyDBContext too as below. It works but I wonder if its in line with good practices. I am not able to find link for guidelines that addresses this issue in entity framework blogs or msdn. Can anyone point me please to the right guidelines?  Thanks.

     

    public MyDBContext(string fileWithPath)
    {
    this.Database.Connection.ConnectionString = GetConnectionString(fileWithPath);
    }
    

     

    <add name="MyDBContext"
        connectionString=""
        providerName="System.Data.SQLite"
       />
    
    Monday, May 16, 2011 1:14 PM
  • Happy to see that, Suresh. And thank you for sharing your solution to us.

     

    have a nice weekend,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 20, 2011 5:32 AM
    Moderator
  • Could you show please GetConnectionString method implementation? Thanks.
    Tuesday, July 23, 2013 1:40 PM