Hello everyone Please help me, I have added a new field in our model, Now I want to run our API project so it updates automatic fields in our database. But in my case no updated fields in our database. This is my code
public static class MigrationsConfiguration
{
public static IApplicationBuilder DbMigrations(this IApplicationBuilder app)
{
using var scope = app.ApplicationServices.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService
var db = scope.ServiceProvider.GetRequiredService
logger.LogInformation("Starting migrations");
db.Database.Migrate();
logger.LogInformation("Db migrations completed");
return app;
}
}

Jignesh KumarRank #91 · 22.9K reputationPosted Jun 18, 2024, 10:43 PM EDT
Hello Sunny,
Please try to add try catch to check any exception in your method,
public static class MigrationsConfiguration { public static IApplicationBuilder ApplyDbMigrations(this IApplicationBuilder app) { using var scope = app.ApplicationServices.CreateScope(); var logger = scope.ServiceProvider.GetRequiredService>(); var db = scope.ServiceProvider.GetRequiredService(); try { logger.LogInformation("Starting database migrations"); db.Database.Migrate(); logger.LogInformation("Database migrations completed successfully"); } catch (Exception ex) { logger.LogError(ex, "An error occurred while applying database migrations"); throw; } return app; } }One more this to check your Configure method is calling this method.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.ApplyDbMigrations(); // Apply your DB migration method here so it will execute }Amit MohantyRank #112 · 20K reputationPosted Jun 18, 2024, 8:17 AM EDT
Ensure that DbMigrations() is called in the startup process of your application.
var builder = WebApplication.CreateBuilder(args); // Other service configurations... var app = builder.Build(); app.DbMigrations(); // Other configurations... app.Run();Sourabh DhimanRank #2,987 · 483 reputationPosted Jun 18, 2024, 8:33 AM EDT
Amit Mohanty I have checked my program.cs file