I have a requirement that i need to work with multiple databases at a time, my requirement is i want to get data from other database and fetch that data into my content page, so i created a new connection in my web.config a tried to call it in my code, the probleme is using peta poco with which is installed with umbraco i think i have _db = ApplicationContext.Current.DatabaseContext.Database for my Umbraco BD ,but if i want to specify another db i don't know which method should i overrid or install another dll of petapoco which is not related to umbraco to specify a connexion but i think that's a bad idea.
(I m using umbraco7.5.10 version and I am using visualstudio2015 and mvc5).
You have to create new object of Database class with another connection string, but be aware about storing this object, we store it in httpContext. Try to use this static method:
public static Database CurrentDb()
{
if (HttpContext.Current.Items["CurrentDb"] == null)
{
var retval = new Database(ConfigurationManager.ConnectionStrings["CustomDatabaseConnection"].ConnectionString, ConfigurationManager.ConnectionStrings["CustomDatabaseConnection"].ProviderName);
HttpContext.Current.Items["CurrentDb"] = retval;
return retval;
}
return (Database)HttpContext.Current.Items["CurrentDb"];
}
I would like to know how did you use httpcontext while ur heriting SurfaceController ?i tried ur solution but i m having an error with HttpContext that need a referencie.
Using Umbraco on multiple databases
Hi All,
I have a requirement that i need to work with multiple databases at a time, my requirement is i want to get data from other database and fetch that data into my content page, so i created a new connection in my web.config a tried to call it in my code, the probleme is using peta poco with which is installed with umbraco i think i have _db = ApplicationContext.Current.DatabaseContext.Database for my Umbraco BD ,but if i want to specify another db i don't know which method should i overrid or install another dll of petapoco which is not related to umbraco to specify a connexion but i think that's a bad idea.
(I m using umbraco7.5.10 version and I am using visualstudio2015 and mvc5).
Thanks&Regards
Youness ben
Hi Youness
You have to create new object of Database class with another connection string, but be aware about storing this object, we store it in httpContext. Try to use this static method:
And then use it like:
Hope it will help you.
Thanks,
Alex
Hi Alex,
I would like to know how did you use httpcontext while ur heriting SurfaceController ?i tried ur solution but i m having an error with HttpContext that need a referencie.
Thanks&Regards
Youness ben
Please add "using System.Web;" to your controller.
Or share code your controller's code, we will look together.
Youness, can you move this method outside Controllers class? It's not right way to use static methods, move it to static class.
And use it in Controller, example:
It works very well thank you Alex!
Glad to help you! Have a nice day!
is working on a reply...