How to configure 2 databases in Umbraco 11
# help-with-umbraco
t
I have the standard Umbraco database as part of the installation of Umbraco 11 and would like to include my own database which stores some data. I added a new connection string in appsettings.json (essentially just copied and pasted the original connection string and renamed it) however when I create the initial migration and then run Update-Database I receive the error The connection string is not initialised. I'm not sure if I need to amend the startup file in Umbraco 11 to include some form of configuration but wonder if anyone can point me in the right direction? Thanks
h
what is the database used for? I normally initialise it in the service that uses it e.g. public class GeoLocationService { private readonly IDatabase _database; public GeoLocationService(IConfiguration appconfig) { _database = new Database(appconfig.GetConnectionString("VantageGeoData"), DatabaseType.SqlServer2012, SqlClientFactory.Instance); }
t
It's a brand new database to hold data for custom pages we create in a custom section. At present I added the below code which gives the same error AFTER creating a migration public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(_config.GetConnectionString("custDb"))); services.AddUmbraco(_env, _config) .AddBackOffice() .AddWebsite() .AddComposers() .Build(); } So in package manager I run Update-Database and then receive The connectionString has not been initialised. So I dont get the option to even create the database yet.
h
Not done that before I'm afraid so can't offer much help, I have only ever connected and consumed data from other databases
t
Ok, now that makes me wonder if I've gone in the wrong direction with this..
h
you can pass the connection string as a parameter in the package manager console Update-Database -Verbose -ConnectionString "CONNECTIONSTRING" -ConnectionProviderName "System.Data.SqlClient" -StartupProjectName WEBSITE_PROJECT -ProjectName MIGRATION_PROJECT
t
I get a parameter cannot be found that matches parameter name ConnectionString. I'm using .Net 7 with Entity Framework Core (7.0.9) if that helps. I'll enable verbose and see if that highlights anything
m
I'm no expert but in any custom sections we've done we've just created the tables within the Umbraco database. Would that be an option for you?
h
it may be -ConnectionStringName possibly, best to check EF docs fro microsoft
t
Ideally we need it separate so other applications can connect to this database in future (other applications won't need access to the Umbraco tables)
I think I may have resolved it by removing the OnConfiguring method for Entity Framework and leaving the startup code as is. So far it seems to have done the trick but gonna have to test a little further. Thanks guys
p
@User May I suggest “people” instead of "guys"? We use gender inclusive language in this Discord. 😀