-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
552dd9f
commit 50cc83b
Showing
3 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using CleanTemplate.Persistence.Db; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace CleanTemplate.Api | ||
{ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
|
||
public interface IMigrationService | ||
{ | ||
void ApplyMigrations(); | ||
} | ||
|
||
public class MigrationService : IMigrationService | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public MigrationService(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public void ApplyMigrations() | ||
{ | ||
using var scope = _serviceProvider.CreateScope(); | ||
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>(); | ||
|
||
// Apply Migration | ||
try | ||
{ | ||
context.Database.Migrate(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Log the error or handle it in some way | ||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>(); | ||
logger.LogError(ex, "An error occurred while migrating the database."); | ||
} | ||
} | ||
} | ||
} |