none
C# Run Time Change SQLite Database Connection String RRS feed

  • Question

  • C# Can I RUN Time Change Database First EF Connection string???

    I Create the C# WPF project for multi machine and database is SQLite 3 Encrypted.

    I use the "ADO.NET Entity data model" "Database First"

    App.config Connection string

    Included sensitive 

    <add name="EFDBEntities" connectionString="metadata=res://*/EFModel.csdl|res://*/EFModel.ssdl|res://*/EFModel.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=E:\MyProject\EFDB.db;password=Pss123&quot;" providerName="System.Data.EntityClient" />

    That excluded sensitive

    <add name="EFDBEntities" connectionString="metadata=res://*/EFModel.csdl|res://*/EFModel.ssdl|res://*/EFModel.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=E:\MyProject\AutoIDIATA\EFDB.db&quot;" providerName="System.Data.EntityClient" />

    I try to add a new class to change connection string ,but it dont work for me.

    public class myContext : DbContext
    {
        public myContext(string conn)
            : base(conn)
        {
    
        }
    }


    Friday, March 22, 2019 1:18 PM

Answers

  • Step: 1
    Add Class
    public class myContext : DbContext
    {
        public myContext(string conn)
            : base(conn)
        {

        }
    }

    Step: 2
    Made New Connection string in application code

    var originalConnectionString = ConfigurationManager.ConnectionStrings["EFDBEntities"].ConnectionString;
    var entityBuilder = new EntityConnectionStringBuilder(originalConnectionString);
    var factory = DbProviderFactories.GetFactory(entityBuilder.Provider);
    var providerBuilder = factory.CreateConnectionStringBuilder();
    providerBuilder.ConnectionString = entityBuilder.ProviderConnectionString;
    providerBuilder.Add("Password", "Mypassword");
    entityBuilder.ProviderConnectionString = providerBuilder.ToString();
    string NewConnectionString = entityBuilder.ToString();

    using (myContextntext db = new myContextntext(NewConnectionString))
    {
         //Code
    }
    • Marked as answer by LouisC27 Saturday, March 23, 2019 5:18 AM
    Saturday, March 23, 2019 5:18 AM

All replies

  • My exception

    using (myContext db = new myContext("data source=E:\MyProject\EFDB.db;password=Pss123"))

           {    // MY Code        }

    Friday, March 22, 2019 1:35 PM
  • Step: 1
    Add Class
    public class myContext : DbContext
    {
        public myContext(string conn)
            : base(conn)
        {

        }
    }

    Step: 2
    Made New Connection string in application code

    var originalConnectionString = ConfigurationManager.ConnectionStrings["EFDBEntities"].ConnectionString;
    var entityBuilder = new EntityConnectionStringBuilder(originalConnectionString);
    var factory = DbProviderFactories.GetFactory(entityBuilder.Provider);
    var providerBuilder = factory.CreateConnectionStringBuilder();
    providerBuilder.ConnectionString = entityBuilder.ProviderConnectionString;
    providerBuilder.Add("Password", "Mypassword");
    entityBuilder.ProviderConnectionString = providerBuilder.ToString();
    string NewConnectionString = entityBuilder.ToString();

    using (myContextntext db = new myContextntext(NewConnectionString))
    {
         //Code
    }
    • Marked as answer by LouisC27 Saturday, March 23, 2019 5:18 AM
    Saturday, March 23, 2019 5:18 AM