Copied to clipboard

Flag this post as spam?

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


  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Aug 17, 2023 @ 10:11
    Rasmus Fjord
    0

    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()
    

    Migration handler looks like this:

     public async Task HandleAsync(UmbracoApplicationStartedNotification notification, CancellationToken cancellationToken)
        {
            IEnumerable<string> pendingMigrations = await _context.Database.GetPendingMigrationsAsync(cancellationToken: cancellationToken);
    
            if (pendingMigrations.Any())
            {
                await _context.Database.MigrateAsync(cancellationToken: cancellationToken);
            }
        }
    

    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.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 17, 2023 @ 13:06
    Sebastiaan Janssen
    0

    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:

    https://github.com/nul800sebastiaan/Cultiv.Hangfire/blob/main/Cultiv.Hangfire/UmbracoBuilderExtensions.cs

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 17, 2023 @ 13:07
    Sebastiaan Janssen
    0

    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.

  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Aug 17, 2023 @ 13:13
    Rasmus Fjord
    0

    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.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 17, 2023 @ 13:15
    Sebastiaan Janssen
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft