Freigeben über

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();
          }
        }
    }
}
Entwicklertechnologien | .NET | Blazor
Entwicklertechnologien | .NET | Blazor
Ein kostenloses und Open-Source-Webframework, mit dem Entwickler Web-Apps mit C# und HTML erstellen können, die von Microsoft entwickelt wurden.
Entwicklertechnologien | ASP.NET | Andere
Entwicklertechnologien | ASP.NET | Andere
Eine Reihe von Technologien in .NET zum Erstellen von Webanwendungen und Webdiensten. Verschiedene Themen, die nicht in bestimmte Kategorien passen.
0 Kommentare Keine Kommentare
{count} Stimmen

Antwort, die vom Frageautor angenommen wurde
  1. Ivan Dragov (CONCENTRIX Corporation) 2,640 Zuverlässigkeitspunkte Externe Microsoft-Mitarbeiter
    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

Ihre Antwort

Antworten können von Fragestellenden als „Angenommen“ und von Moderierenden als „Empfohlen“ gekennzeichnet werden, wodurch Benutzende wissen, dass diese Antwort das Problem des Fragestellenden gelöst hat.