ASP.NET Web API Connection String

4 posts, 0 answers
  1. Paul
    Paul avatar
    113 posts
    Member since:
    May 2009

    Posted 01 May 2013 Link to this post

    Hi,
        How do you set the ASP.NET Web API connection string at runtime instead of it using the web.config file. In domain services this could be done creating a partial class of the domaincontext and overriding the create context method but how is this done with the WebApi once the ORM wizard has run.

    Thanks
  2. Yordan
    Admin
    Yordan avatar
    39 posts

    Posted 03 May 2013 Link to this post

    Hi Paul,

    There are two possible ways to handle this situation:

    1) If your model (.RLINQ) file is used only with the ASP.NET Web API project you can override the Init method of the model class and than pass the new connection string. The code is similar to:
    protected override void Init(string connectionString, BackendConfiguration backendConfiguration, MetadataContainer metadataContainer)
    {
        // Insert your logic here.
        connectionString = "[your conn string]";
     
        base.Init(connectionString, backendConfiguration, metadataContainer);
    }

    2) If your model (.RLINQ) is used by other projects as well maybe it is not suitable to pass new connection string in the Init method. If that is the case than the OpenAccessBaseRepository class have to be modified. First comment out the line protected TContext dataContext = new TContext();. Second add variable of your model type and in the constructor use the one that accepts connection string. The code is like the following:
    //protected TContext dataContext = new TContext();
     
    protected OpenAccessModel.EntitiesModel dataContext = new OpenAccessModel.EntitiesModel(GetConnectionsString());
     
    private static string GetConnectionsString()
    {
        // Insert your logic here
        return "";
    }

    Please find the attached project as an example. The changes are in classes OpenAccessBaseRepository.cs and EntitiesModel.cs.

    If you encounter any difficulties implementing any of the fore-mentioned scenarios do not hesitate to contact us again.
     
    Regards,
    Yordan
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  3. DevCraft banner
  4. Paul
    Paul avatar
    113 posts
    Member since:
    May 2009

    Posted 03 May 2013 Link to this post

    Hi Yordan,
                     Thanks this was exactly what I was after. Tested both and they worked spot on. I went for the first one in the end as I could create a partial class of the entitymodel and override the init there. That way I know the setting will not get overridden if any wizards run.

    Thanks again.
  5. Yordan
    Admin
    Yordan avatar
    39 posts

    Posted 03 May 2013 Link to this post

    Hi Paul,

    I am glad to find out that the proposed solution works fine in your scenario. If there are any other issues that we can help you to overcome please get back to us again.

    Regards,
    Yordan
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Back to Top