How to create a DbContext instance of the specified database type












0















I know that the default return here is the SQL Server database type, but I need the MySQL database type, the problem is to create a DbContext instance of the specified database type?



Of course, I know that it can also be created by specifying the connectionName, but the database connection string is encrypted and the string needs to be decrypted



The pseudo code is as follows:



public partial class MyDbContext : DbContext
{
static string mysqlConn = ReadConnectionString(); //Get the encrypted MySQL connection string

public MyDbContext() : base(mysqlConn)
{
// Even if I want to modify the connection string in this way, nor will it throw a connection string malformed exception
Database.Connection.ConnectionString = DecryptConnectionString(mysqlConn);
}
}









share|improve this question

























  • What framework and version do you use ?

    – Serg Sh
    Dec 30 '18 at 14:21











  • .NET Framework 4.5 + EntityFramework 6

    – win10shit
    Dec 30 '18 at 16:09


















0















I know that the default return here is the SQL Server database type, but I need the MySQL database type, the problem is to create a DbContext instance of the specified database type?



Of course, I know that it can also be created by specifying the connectionName, but the database connection string is encrypted and the string needs to be decrypted



The pseudo code is as follows:



public partial class MyDbContext : DbContext
{
static string mysqlConn = ReadConnectionString(); //Get the encrypted MySQL connection string

public MyDbContext() : base(mysqlConn)
{
// Even if I want to modify the connection string in this way, nor will it throw a connection string malformed exception
Database.Connection.ConnectionString = DecryptConnectionString(mysqlConn);
}
}









share|improve this question

























  • What framework and version do you use ?

    – Serg Sh
    Dec 30 '18 at 14:21











  • .NET Framework 4.5 + EntityFramework 6

    – win10shit
    Dec 30 '18 at 16:09
















0












0








0








I know that the default return here is the SQL Server database type, but I need the MySQL database type, the problem is to create a DbContext instance of the specified database type?



Of course, I know that it can also be created by specifying the connectionName, but the database connection string is encrypted and the string needs to be decrypted



The pseudo code is as follows:



public partial class MyDbContext : DbContext
{
static string mysqlConn = ReadConnectionString(); //Get the encrypted MySQL connection string

public MyDbContext() : base(mysqlConn)
{
// Even if I want to modify the connection string in this way, nor will it throw a connection string malformed exception
Database.Connection.ConnectionString = DecryptConnectionString(mysqlConn);
}
}









share|improve this question
















I know that the default return here is the SQL Server database type, but I need the MySQL database type, the problem is to create a DbContext instance of the specified database type?



Of course, I know that it can also be created by specifying the connectionName, but the database connection string is encrypted and the string needs to be decrypted



The pseudo code is as follows:



public partial class MyDbContext : DbContext
{
static string mysqlConn = ReadConnectionString(); //Get the encrypted MySQL connection string

public MyDbContext() : base(mysqlConn)
{
// Even if I want to modify the connection string in this way, nor will it throw a connection string malformed exception
Database.Connection.ConnectionString = DecryptConnectionString(mysqlConn);
}
}






c# entity-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 21:46









marc_s

574k12811091256




574k12811091256










asked Dec 29 '18 at 18:00









win10shitwin10shit

12




12













  • What framework and version do you use ?

    – Serg Sh
    Dec 30 '18 at 14:21











  • .NET Framework 4.5 + EntityFramework 6

    – win10shit
    Dec 30 '18 at 16:09





















  • What framework and version do you use ?

    – Serg Sh
    Dec 30 '18 at 14:21











  • .NET Framework 4.5 + EntityFramework 6

    – win10shit
    Dec 30 '18 at 16:09



















What framework and version do you use ?

– Serg Sh
Dec 30 '18 at 14:21





What framework and version do you use ?

– Serg Sh
Dec 30 '18 at 14:21













.NET Framework 4.5 + EntityFramework 6

– win10shit
Dec 30 '18 at 16:09







.NET Framework 4.5 + EntityFramework 6

– win10shit
Dec 30 '18 at 16:09














3 Answers
3






active

oldest

votes


















0














Ok, it's about what I expected, few of my questions can be answered.



Although I am not too familiar with EntityFramework, by looking at the source code of DbContext, I can almost certainly determine that this is a design flaw, it lacks a constructor like this:





Public DbContext(string connectionString, string providerName)





share|improve this answer































    0














    links below contais all essential snippets



    Code based configuration EF6



    MySql specific configuration EF6






    share|improve this answer
























    • Thank you for taking the time to study this problem, i have a long comment, exceeding the length limit

      – win10shit
      Dec 31 '18 at 10:39





















    0














    To Serg Sh
    Thank you for taking the time to study this problem. I saw the solution you provided. By constructing the connection string, the final method is still:




    Public DbContext(string nameOrConnectionString)


    As I said before, this method returns the SQLServer database type. I insist that there is a problem with the DbContext design. Microsoft should delete this method and implement the following method groups:





    //name of the connection string in the configuration file, Will automatically create a specific type of database connection based on the providerName of the configuration file
    Public DbContext (string connectionName)


    //connection string, the default returns SQLServer database type, namely: providerName = "System.Data.SqlClient"
    Public DbContext (string connectionString)



    //Connection string and data provider name
    Public DbContext(string connectionString, string providerName)


    If this is not the case, you can't escape the trap of the DbContext constructor.



    In fact, I already have a plan that I don't want to use before asking this question:
    Use a normal data connection string, feel free to fill in the parameters, read it and replace it with the actual string that has been decrypted and stored elsewhere.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972041%2fhow-to-create-a-dbcontext-instance-of-the-specified-database-type%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Ok, it's about what I expected, few of my questions can be answered.



      Although I am not too familiar with EntityFramework, by looking at the source code of DbContext, I can almost certainly determine that this is a design flaw, it lacks a constructor like this:





      Public DbContext(string connectionString, string providerName)





      share|improve this answer




























        0














        Ok, it's about what I expected, few of my questions can be answered.



        Although I am not too familiar with EntityFramework, by looking at the source code of DbContext, I can almost certainly determine that this is a design flaw, it lacks a constructor like this:





        Public DbContext(string connectionString, string providerName)





        share|improve this answer


























          0












          0








          0







          Ok, it's about what I expected, few of my questions can be answered.



          Although I am not too familiar with EntityFramework, by looking at the source code of DbContext, I can almost certainly determine that this is a design flaw, it lacks a constructor like this:





          Public DbContext(string connectionString, string providerName)





          share|improve this answer













          Ok, it's about what I expected, few of my questions can be answered.



          Although I am not too familiar with EntityFramework, by looking at the source code of DbContext, I can almost certainly determine that this is a design flaw, it lacks a constructor like this:





          Public DbContext(string connectionString, string providerName)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 30 '18 at 9:47









          win10shitwin10shit

          12




          12

























              0














              links below contais all essential snippets



              Code based configuration EF6



              MySql specific configuration EF6






              share|improve this answer
























              • Thank you for taking the time to study this problem, i have a long comment, exceeding the length limit

                – win10shit
                Dec 31 '18 at 10:39


















              0














              links below contais all essential snippets



              Code based configuration EF6



              MySql specific configuration EF6






              share|improve this answer
























              • Thank you for taking the time to study this problem, i have a long comment, exceeding the length limit

                – win10shit
                Dec 31 '18 at 10:39
















              0












              0








              0







              links below contais all essential snippets



              Code based configuration EF6



              MySql specific configuration EF6






              share|improve this answer













              links below contais all essential snippets



              Code based configuration EF6



              MySql specific configuration EF6







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 31 '18 at 9:11









              Serg ShSerg Sh

              18119




              18119













              • Thank you for taking the time to study this problem, i have a long comment, exceeding the length limit

                – win10shit
                Dec 31 '18 at 10:39





















              • Thank you for taking the time to study this problem, i have a long comment, exceeding the length limit

                – win10shit
                Dec 31 '18 at 10:39



















              Thank you for taking the time to study this problem, i have a long comment, exceeding the length limit

              – win10shit
              Dec 31 '18 at 10:39







              Thank you for taking the time to study this problem, i have a long comment, exceeding the length limit

              – win10shit
              Dec 31 '18 at 10:39













              0














              To Serg Sh
              Thank you for taking the time to study this problem. I saw the solution you provided. By constructing the connection string, the final method is still:




              Public DbContext(string nameOrConnectionString)


              As I said before, this method returns the SQLServer database type. I insist that there is a problem with the DbContext design. Microsoft should delete this method and implement the following method groups:





              //name of the connection string in the configuration file, Will automatically create a specific type of database connection based on the providerName of the configuration file
              Public DbContext (string connectionName)


              //connection string, the default returns SQLServer database type, namely: providerName = "System.Data.SqlClient"
              Public DbContext (string connectionString)



              //Connection string and data provider name
              Public DbContext(string connectionString, string providerName)


              If this is not the case, you can't escape the trap of the DbContext constructor.



              In fact, I already have a plan that I don't want to use before asking this question:
              Use a normal data connection string, feel free to fill in the parameters, read it and replace it with the actual string that has been decrypted and stored elsewhere.






              share|improve this answer




























                0














                To Serg Sh
                Thank you for taking the time to study this problem. I saw the solution you provided. By constructing the connection string, the final method is still:




                Public DbContext(string nameOrConnectionString)


                As I said before, this method returns the SQLServer database type. I insist that there is a problem with the DbContext design. Microsoft should delete this method and implement the following method groups:





                //name of the connection string in the configuration file, Will automatically create a specific type of database connection based on the providerName of the configuration file
                Public DbContext (string connectionName)


                //connection string, the default returns SQLServer database type, namely: providerName = "System.Data.SqlClient"
                Public DbContext (string connectionString)



                //Connection string and data provider name
                Public DbContext(string connectionString, string providerName)


                If this is not the case, you can't escape the trap of the DbContext constructor.



                In fact, I already have a plan that I don't want to use before asking this question:
                Use a normal data connection string, feel free to fill in the parameters, read it and replace it with the actual string that has been decrypted and stored elsewhere.






                share|improve this answer


























                  0












                  0








                  0







                  To Serg Sh
                  Thank you for taking the time to study this problem. I saw the solution you provided. By constructing the connection string, the final method is still:




                  Public DbContext(string nameOrConnectionString)


                  As I said before, this method returns the SQLServer database type. I insist that there is a problem with the DbContext design. Microsoft should delete this method and implement the following method groups:





                  //name of the connection string in the configuration file, Will automatically create a specific type of database connection based on the providerName of the configuration file
                  Public DbContext (string connectionName)


                  //connection string, the default returns SQLServer database type, namely: providerName = "System.Data.SqlClient"
                  Public DbContext (string connectionString)



                  //Connection string and data provider name
                  Public DbContext(string connectionString, string providerName)


                  If this is not the case, you can't escape the trap of the DbContext constructor.



                  In fact, I already have a plan that I don't want to use before asking this question:
                  Use a normal data connection string, feel free to fill in the parameters, read it and replace it with the actual string that has been decrypted and stored elsewhere.






                  share|improve this answer













                  To Serg Sh
                  Thank you for taking the time to study this problem. I saw the solution you provided. By constructing the connection string, the final method is still:




                  Public DbContext(string nameOrConnectionString)


                  As I said before, this method returns the SQLServer database type. I insist that there is a problem with the DbContext design. Microsoft should delete this method and implement the following method groups:





                  //name of the connection string in the configuration file, Will automatically create a specific type of database connection based on the providerName of the configuration file
                  Public DbContext (string connectionName)


                  //connection string, the default returns SQLServer database type, namely: providerName = "System.Data.SqlClient"
                  Public DbContext (string connectionString)



                  //Connection string and data provider name
                  Public DbContext(string connectionString, string providerName)


                  If this is not the case, you can't escape the trap of the DbContext constructor.



                  In fact, I already have a plan that I don't want to use before asking this question:
                  Use a normal data connection string, feel free to fill in the parameters, read it and replace it with the actual string that has been decrypted and stored elsewhere.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 10:31









                  win10shitwin10shit

                  12




                  12






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972041%2fhow-to-create-a-dbcontext-instance-of-the-specified-database-type%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'