Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi! I'm fairly new to Umbraco and struggle a bit to get my head around how to retrieve data from a external database.
I've been able to access that within the same database following this:
https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core
I've added a second connection string into the appsettings.json pointing to a sqllite db.
Then I've added the connection string to builder.Services:
using Microsoft.EntityFrameworkCore; using bussbokning.Data; using bussbokning.Controllers; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); var env = builder.Environment; var config = builder.Configuration; builder.Services.AddUmbraco(env, config) .AddBackOffice() .AddWebsite() .AddComposers() .AddDeliveryApi() .Build(); string connectionString = config.GetConnectionString("umbracoDbDSN2"); string connectionStringProvider = config.GetConnectionStringProviderName("umbracoDbDSN2"); Console.WriteLine("connectionString: " + connectionString); Console.WriteLine("connectionStringProvider: " + connectionStringProvider); builder.Services.AddUmbracoDbContext<DrivingOrderContext>(options => options.UseSqlite(connectionString)); WebApplication app = builder.Build(); await app.BootUmbracoAsync(); app.UseUmbraco() .WithMiddleware(u => { u.UseBackOffice(); u.UseWebsite(); }) .WithEndpoints(u => { u.UseInstallerEndpoints(); u.UseBackOfficeEndpoints(); u.UseWebsiteEndpoints(); }); await app.RunAsync();
But the controller still takes the defualt connection.
Clearly something that I don't understand, hope someone can point me in the right direction. Maybe this can be done better in some other way?
Hi! I know this post is a few months old, but I posted a bug report about this issue as it was recently a blocker for us too: https://github.com/umbraco/Umbraco-CMS/issues/16805
HQ disagree that it's a bug and have suggested using the standard MS way of adding a DB context.
Hope that helps!
My inclination would be to have the connection string for the second database embedded directly in the query you're using for the data query?
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Gettting data from external database.
Hi! I'm fairly new to Umbraco and struggle a bit to get my head around how to retrieve data from a external database.
I've been able to access that within the same database following this:
https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core
I've added a second connection string into the appsettings.json pointing to a sqllite db.
Then I've added the connection string to builder.Services:
But the controller still takes the defualt connection.
Clearly something that I don't understand, hope someone can point me in the right direction. Maybe this can be done better in some other way?
Hi! I know this post is a few months old, but I posted a bug report about this issue as it was recently a blocker for us too: https://github.com/umbraco/Umbraco-CMS/issues/16805
HQ disagree that it's a bug and have suggested using the standard MS way of adding a DB context.
Hope that helps!
My inclination would be to have the connection string for the second database embedded directly in the query you're using for the data query?
is working on a reply...