web app failed at database migration

Jürgen Keitzl 61 Zuverlässigkeitspunkte
2024-04-26T15:33:59.45+00:00

Hello,

 

I'm learning  Blazor ( do it as an hobby) and created my first WebApp.

No I want to publish the app on a web-server (Microsoftserver) and running in problems.

 

I followed all the necessery steps in the right order (thanks to CodingWithJannick on Youtube)

 

But if the app starts, it failed at the creation of the databes tables, means the database migration doesn't work. The logfile shows  the error in the line

       
 _Db.Database.Migrate();
       
         

of the code (below).

On my local computer it works. After starting the app create the new database with the tables.

 

The connection string is ok, DbContext is also ok.

Database user has the rights to create a database. 

 

What could be the mistake which I do? 

Thanks for Tipps and help.

 

Juergen

 

Program.cs
...
ApplyMigration;
appRun;
void ApplyMigration()
{  
    using (var  scope = app.Services.CreateScope())
    {
      var _Db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
        if(_Db!=null)
        {
          if(_Db.Database.GetMigrations().Any())
          {   
            _Db.Database.Migrate();
          }
        }
    }
}
Blazor
Blazor
Ein kostenloses Open-Source-Webframework, mit dem Entwickler Web-Apps mit C# und HTML erstellen können, die von Microsoft entwickelt werden.
4 Fragen
ASP.NET
ASP.NET
Eine Zusammenstellung von Technologien in Microsoft .NET Framework zum Erstellen von Webanwendungen und XML-Webdiensten.
2 Fragen
0 Kommentare Keine Kommentare
{count} Stimmen

Akzeptierte Antwort
  1. Ivan Dragov (CONCENTRIX Corporation) 1.460 Zuverlässigkeitspunkte Microsoft-Anbieter
    2024-04-27T10:22:45.0266667+00:00

    Hallo Jürgen,

    Es wird immer empfohlen, die genaue Fehlermeldung anzugeben, die Du erhalten hast. Verwendest Du die WebApplication.CreateBuilder-Methode, um eine WebApplication-Instanz zu erstellen, und registrierst Du anschließend Deinen DbContext mithilfe der Services.AddDbContext-Methode? Diese beiden Methoden sollten vor Deiner Service.CreateScopeMethode platziert werden, wie hier beschrieben:

    .NET 6 how to run Migration automatically in program.cs

    Gruß,

    Ivan Dragov


0 zusätzliche Antworten

Sortieren nach: Am hilfreichsten