V12.1.1 EF Core context wont map to production database
Hey!
So i have a solution ive been building on latest Umbraco 12, and with the new EF implementation.
Everything is working just fine in local, when i run it with an SQLLITE db, everything is smooth, migrations get run etc.
Now i deployed it, and im using just a regular MSSQL db, Umbraco is installed, but EF core wont run migrations. Comes with a wierd error.
This is my startup.cs
var conString = _config.GetSection("ConnectionStrings:umbracoDbDSN").Value;
var providerName = _config.GetSection("ConnectionStrings:umbracoDbDSN_ProviderName").Value;
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddWebsite()
.AddDeliveryApi()
.AddComposers()
.AddCustomServices()
.Build();
services.AddUmbracoEFCoreContext<Context>(conString, providerName);
Ive tried moving the AddUmbracoEFCoreContext before the "addUmbraco" but still dosnt fix it.
So the sites boots up fine, I can work in umbraco just dosnt run migrations and cant interact with the EF.
The error is:
System.AggregateException: One or more errors occurred. (One or more errors occurred. (The ConnectionString property has not been initialized.))\r\n ---> System.AggregateException: One or more errors occurred. (The ConnectionString property has not been initialized.)\r\n ---> System.InvalidOperationException: The ConnectionString property has not been initialized.\r\n at Microsoft.Data.SqlClient.SqlConnection.PermissionDemand()\r\n at Microsoft.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)\r\n at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)\r\n at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)\r\n at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)\r\n at Microsoft.Data.SqlClient.SqlConnection.InternalOpenAsync(CancellationToken cancellationToken)\r\n--- End of stack trace from previous location ---\r\n at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)\r\n at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass20_0.<<ExistsAsync>b__0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.ExistsAsync(CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrationsAsync(CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetAppliedMigrationsAsync(DatabaseFacade databaseFacade, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetPendingMigrationsAsync(DatabaseFacade databaseFacade, CancellationToken cancellationToken)\r\n at XXX.DataLayer.MigrationHandler.MigrationHandler.HandleAsync(UmbracoApplicationStartedNotification notification, CancellationToken cancellationToken) in C:\\actions-runner\\_work\\XXX\\DataLayer\\MigrationHandler\\MigrationHandler.cs:line 19\r\n at Umbraco.Cms.Core.Events.INotificationAsyncHandler`1.HandleAsync(IEnumerable`1 notifications, CancellationToken cancellationToken)\r\n at Umbraco.Cms.Core.Events.EventAggregator.PublishCoreAsync[TNotification](IEnumerable`1 allHandlers, IEnumerable`1 notifications, CancellationToken cancellationToken)\r\n at Umbraco.Cms.Core.Events.NotificationAsyncHandlerWrapperImpl`1.HandleAsync[TNotification,TNotificationHandler](IEnumerable`1 notifications, CancellationToken cancellationToken, ServiceFactory serviceFactory, Func`4 publish)\r\n at Umbraco.Cms.Core.Events.EventAggregator.PublishNotificationsAsync[TNotification,TNotificationHandler](IEnumerable`1 notifications, CancellationToken cancellationToken)\r\n --- End of inner exception stack trace ---\r\n at System.Threading.Tasks.Task.WaitAllCore(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)\r\n at System.Threading.Tasks.Task.WaitAll(Task[] tasks)\r\n at Umbraco.Cms.Core.Events.EventAggregator.Publish[TNotification,TNotificationHandler](IEnumerable`1 notifications)\r\n at Umbraco.Cms.Core.Events.EventAggregator.Publish[TNotification](TNotification notification)\r\n at Umbraco.Cms.Infrastructure.Runtime.CoreRuntime.<StartAsync>b__22_1()\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n--- End of stack trace from previous location ---\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)\r\n --- End of inner exception stack trace ---\r\n at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)\r\n at Microsoft.Extensions.Hosting.Internal.ApplicationLifetime.NotifyStarted()
Ive been following the offical guide, but I think its because that SQLlite and MSSQL interact differently so maybe i need to setup something differently.
It should work (from v12.0.0) if you connection string is Data Source=|DataDirectory|/Umbraco.sqlite.db;Cache=Shared;Foreign Keys=True;Pooling=True - see https://github.com/umbraco/Umbraco-CMS/pull/14278 for how this is implemented.
But I think the connection string might not be available in that part of the startup.cs?
services.AddUmbracoEFCoreContext<Context>("{YOUR CONNECTIONSTRING HERE}", "{YOUR PROVIDER NAME HERE}");
Should work.
If that works, then I need to do some funky stuff for Hangfire, you might need to do the same:
Of course you SHOULD allow SQLite, where I am explicitly crashing here because Hangfire doesn't work with SQLite. The point is more the way I get the connection string in this code.
Actually it works fine with sqllite... But as soon as it's a mssql connection string it does not.
So locally I can run it fine with sqllite. When moving to the prod server and switching to the mssql this error occurs. Umbraco runs super fine on the mssql server but for some reason efcore just falls flat.
Hmmm try with the hardcoded connection string as well then on live? Not sure how you set your connection string either, if it's environment variables, then not sure if the _config.GetSection works.
V12.1.1 EF Core context wont map to production database
Hey!
So i have a solution ive been building on latest Umbraco 12, and with the new EF implementation.
Everything is working just fine in local, when i run it with an SQLLITE db, everything is smooth, migrations get run etc.
Now i deployed it, and im using just a regular MSSQL db, Umbraco is installed, but EF core wont run migrations. Comes with a wierd error.
This is my startup.cs
Ive tried moving the AddUmbracoEFCoreContext before the "addUmbraco" but still dosnt fix it.
So the sites boots up fine, I can work in umbraco just dosnt run migrations and cant interact with the EF.
The error is:
Migration handler looks like this:
Ive been following the offical guide, but I think its because that SQLlite and MSSQL interact differently so maybe i need to setup something differently.
It should work (from v12.0.0) if you connection string is
Data Source=|DataDirectory|/Umbraco.sqlite.db;Cache=Shared;Foreign Keys=True;Pooling=True
- see https://github.com/umbraco/Umbraco-CMS/pull/14278 for how this is implemented.But I think the connection string might not be available in that part of the startup.cs?
Should work.
If that works, then I need to do some funky stuff for Hangfire, you might need to do the same:
https://github.com/nul800sebastiaan/Cultiv.Hangfire/blob/main/Cultiv.Hangfire/UmbracoBuilderExtensions.cs
Of course you SHOULD allow SQLite, where I am explicitly crashing here because Hangfire doesn't work with SQLite. The point is more the way I get the connection string in this code.
Thx for answering Seb ❤️
Actually it works fine with sqllite... But as soon as it's a mssql connection string it does not.
So locally I can run it fine with sqllite. When moving to the prod server and switching to the mssql this error occurs. Umbraco runs super fine on the mssql server but for some reason efcore just falls flat.
No worries!
Hmmm try with the hardcoded connection string as well then on live? Not sure how you set your connection string either, if it's environment variables, then not sure if the
_config.GetSection
works.is working on a reply...