none
Dynamic connection string for entity framework code first EF6 RRS feed

  • Question

  • I am trying to generate connection string dynamically to passing as parameter in dbContext constructor.

    Generating dynamic connection string based on selection:

     switch (providerName)
                {
                    case "System.Data.SqlClient":
                        conn = new SqlConnectionStringBuilder(entityBuilder.ProviderConnectionString)
                        {
    
                            UserID = databaseUserId,
                            Password = databasePassword,
                            DataSource = serverName,
                            IntegratedSecurity = false,
                            InitialCatalog = databaseName
    
                        }.ConnectionString;
    
                        break;
                    case "MySql.Data.MySqlClient":
                        conn = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder(entityBuilder.ProviderConnectionString)
                        {
                            Server = serverName,
                            UserID = databaseUserId,
                            Password = databasePassword,
                            Database = databaseName,
                            PersistSecurityInfo = true
    
                        }.ConnectionString;
                        break;
                    default:
                        break;
                }
    Passing connection string to constructor: 
      public partial class ModelCodeFist : DbContext
        {
            public ModelCodeFist(string connection)
                : base(connection)
            {
            }
    }

    <entityFramework> <defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers> </entityFramework>




     <DbProviderFactories>
          <remove invariant="MySql.Data.MySqlClient" />
          <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        </DbProviderFactories>

    Getting error:

    Failed to set Database.DefaultConnectionFactory to an instance of the 'MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6' type as specified in the application configuration.

    Let me know if i miss something ,

    Thanks!

    Monday, May 15, 2017 11:14 AM

All replies

  • Hi Narendra-GlobalSysInfo,

    According to your description and related error message, it seems that DbContext want to retrieve SQL connection from configuration file, but it connection name does not match the configuration connection name.

    Please check if you pass the right connection string when you use DbContext. like this:

    EntityConnection entityConn =DBConnectionHelper.BuildConnection();
    using (var db = new ModelCodeFist(entityConn.ConnectionString))
    {
    ....
    }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 19, 2017 1:46 AM
    Moderator
  • Hi  Cole,

    I am generating connection string dynamically for mysql/mssql ( In this scenario it's mysql ) , but not able to change default connection factory. By default it's sql and that's the reason maybe it's generating error.

    How i able to change default connection factory at runtime ?

    Please refer this link, and let me know.

    Thanks!

    Github link

    Change default connection factory

    Friday, May 19, 2017 1:55 AM
  • Hi Narendra-GlobalSysInfo,

    As I mentioned before, you need to pass connection string runtime when you use DbContext. Could you please share a simple complete demo, which could reproduce the issue on my side via OneDrive.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 19, 2017 5:13 AM
    Moderator
  • Hi Cole,

    Thanks for continous support, 

    You can check sample code here Github and here Stackoverflow 

    1. How i suppose to update the default connection factory to mysql/oracle/mssql(default) at runtime EF6 code first.

    2. Is there a way to update connecting string( which we get from web.config ) for dbContext as param [ CODE @FIRST ]see sample code below

    Please let me know,need help on this. 

    Thanks!


    Monday, May 22, 2017 2:21 AM
  • Hi Narendra,

    Please pass connection string to Dbcontext when create the Dbcontext instance, like this:

    public DbContext alterConnectionString (string connectionString){
    
     var getConnectionString = db.ConnectionString ; //getting a connection string
         
        DbContext db = new  DbContext(getConnectionString );     
        return  db;
    }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 23, 2017 6:07 AM
    Moderator