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?

Please Sign in or register to post replies

Write your reply to:

Draft