Skip to content

An error occurred while initialising the database when using postgres as db #1319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
amira133 opened this issue Apr 11, 2025 · 5 comments
Open

Comments

@amira133
Copy link

amira133 commented Apr 11, 2025

Describe the bug
hi , I create a Angular project using postgres as db and ran it, but I got this error: An error occurred while initialising the database..

I'm sure my connection string and database are properly configured since my other ASP.NET projects are running fine with the exact same connection string right now.

To Reproduce
Steps to reproduce the behavior:

  1. dotnet new install Clean.Architecture.Solution.Template::9.0.8
  2. dotnet new ca-sln --client-framework Angular --output YourProjectName --database postgresql
  3. set connection string for DB
  4. cd src/Web
  5. dotnet run
  6. show this error
NSwag command line tool for .NET Core Net90, toolchain v14.2.0.0 (NJsonSchema v11.1.0.0 (Newtonsoft.Json v13.0.0.0));
Visit http://NSwag.org for more information.;
NSwag bin directory: XXX\.nuget\packages\nswag.msbuild\14.2.0\tools\Net90;
Executing file 'config.nswag' with variables 'Configuration=Debug'...;
Launcher directory: XXX\.nuget\packages\nswag.msbuild\14.2.0\tools\Net90;fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
   An error occurred using the connection to database 'YourProjectName2Db' on server 'tcp://127.0.0.1:5432'.;
   fail: YourProjectName2.Infrastructure.Data.ApplicationDbContextInitialiser[0]
   An error occurred while initialising the database.
   System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ApplicationDbContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
      at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`1.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, TParam arg)
      at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType)
      at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
      at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken);System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.; ---> System.AggregateException: One or more errors occurred. (An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ApplicationDbContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.); ---> System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ApplicationDbContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`1.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, TParam arg)
at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
at YourProjectName2.Infrastructure.Data.ApplicationDbContextInitialiser.InitialiseAsync() in D:\Projects\DotNetStarterKit\4\YourProjectName2\src\Infrastructure\Data\ApplicationDbContextInitialiser.cs:line 45
at YourProjectName2.Infrastructure.Data.InitialiserExtensions.InitialiseDatabaseAsync(WebApplication app) in D:\Projects\DotNetStarterKit\4\YourProjectName2\src\Infrastructure\Data\ApplicationDbContextInitialiser.cs:line 20
at Program.<Main>$(String[] args) in D:\Projects\DotNetStarterKit\4\YourProjectName2\src\Web\Program.cs:line 16
at Program.<Main>(String[] args)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at NSwag.Commands.ServiceProviderResolver.GetServiceProviderWithHostFactoryResolver(Assembly assembly) in /_/src/NSwag.Commands/HostApplication.cs:line 171
at NSwag.Commands.ServiceProviderResolver.GetServiceProvider(Assembly assembly) in /_/src/NSwag.Commands/HostApplication.cs:line 70
at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 27
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
--- End of inner exception stack trace ---
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in /_/src/NSwag.AspNetCore.Launcher/Program.cs:line 132
      at YourProjectName2.Infrastructure.Data.ApplicationDbContextInitialiser.InitialiseAsync() in D:\Projects\DotNetStarterKit\4\YourProjectName2\src\Infrastructure\Data\ApplicationDbContextInitialiser.cs:line 45;System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.
at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 195
at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in /_/src/NSwag.Commands/NSwagDocumentBase.cs:line 270
at NSwag.Commands.NSwagDocument.ExecuteAsync() in /_/src/NSwag.Commands/NSwagDocument.cs:line 67
at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 76
at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 33
at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor.cs:line 65```

@takecarehn
Copy link

Do you solve this bug?

@SniperChan
Copy link

I have the same problem,have you solved the problem?

@amira133
Copy link
Author

Unfortunately, I couldn't fix the problem. If you find a solution, please post it here.

@PhillipWayne
Copy link

Hello everyone,

I have found a small workaround for myself.
I think it has something to do with the initial migration.

It worked for me as follows:

  • Delete migration & snapshot.
  • Comment out or delete Nswag config from the MSBuild configuration.
  • Create migration
  • Add NSwag MSBuild config again.
  • Start the application.

After that everything worked for me again.

@rocky-upadhaya
Copy link

I got the similar error while building I did following step solve

  1. Made sure the credntials is correct.
  2. create a database named 'yourprojectname' (all small letter) .
  3. Make sure the db name matches in the config file. DB name was case sensetive in my case.
  4. Update-Database the initial Migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants