Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • code-tensify 1 post 21 karma points
    Mar 22, 2024 @ 10:56
    code-tensify
    0

    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:

    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?

  • Alex Wilks 1 post 21 karma points
    Aug 02, 2024 @ 10:51
    Alex Wilks
    0

    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!

  • Steve Brereton 32 posts 173 karma points
    Aug 05, 2024 @ 13:28
    Steve Brereton
    0

    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?

Please Sign in or register to post replies

Write your reply to:

Draft