Base solution for your next web application
Open Closed

Dynamic Connection String #335


User avatar
0
stevenmunoz created

hi !

how can i send user and password as parameters in my web.config connection string, using abp ? i’ve done with asp.net creating sql connection before open, but here it’s a little bit different.

Thanks for your help


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Want to use different connection based on current user or some other dynamic conditions? Have you more than one db? If so, actually all the thing is done in your DbContext's constructor. You can write such a constructor

    public class MyDbContext : AbpDbContext
            {
                public MyDbContext(string nameOrConnectionString)
                    : base(CreateDynamicConnectionString())
                {
                }
    
                private static string CreateDynamicConnectionString()
                {
                    //TODO: create and return some dynamic conn string.
                }
            }
    

    string nameOrConnectionString argument should be remain, because ABP uses it.

  • User Avatar
    0
    stevenmunoz created

    This is how we did it with no success :( ...

    public Backend_MintDbContext(string nameOrConnectionString) : base(CreateDynamicConnectionString()) {

        }
    

    private static string CreateDynamicConnectionString() { return "DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx.xx.xxx.xxx)(PORT=xxxx)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxxx)));PASSWORD=xxxx;PERSIST SECURITY INFO=True;USER ID=xxxx; enlist=dynamic;"; }

    This is our currently working default connection string, we are using the same connection string on above method

    <connectionStrings> <add name="Default" connectionString="DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx.xx.xxx.xxx)(PORT=xxxx)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxxx)));PASSWORD=xxxx;PERSIST SECURITY INFO=True;USER ID=xxxx; enlist=dynamic;" providerName="Oracle.ManagedDataAccess.Client" /> </connectionStrings>

    And we remove this constructor

    public Backend_MintDbContext() : base("Default") {

        }
    

    So it's not working in that way ... what can be wrong ? thanks for your help

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I did not understand well what you exactly want :) Should you replace user name and pass in CreateDynamicConnectionString method? You are returning a constant string, not dynamically created?

  • User Avatar
    0
    stevenmunoz created

    Yes you're right

    The first thing i was trying to test, is get connection with database just with the same connection string using the method constructor with nameOrConnectionString param, but i can't connect, as i described in above post, so the next step is pass user and password as parameters, in fact i don't know how neither.

    Thanks a lot for your help