This is a migrated thread and some comments may be shown as answers.

ASP.NET Web API Connection String

3 Answers 104 Views
Web Services
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paul
Top achievements
Rank 1
Paul asked on 01 May 2013, 09:17 AM
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

3 Answers, 1 is accepted

Sort by
0
Yordan
Telerik team
answered on 03 May 2013, 10:29 AM
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 >>
0
Paul
Top achievements
Rank 1
answered on 03 May 2013, 11:04 AM
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.
0
Yordan
Telerik team
answered on 03 May 2013, 12:51 PM
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 >>
Tags
Web Services
Asked by
Paul
Top achievements
Rank 1
Answers by
Yordan
Telerik team
Paul
Top achievements
Rank 1
Share this question
or