An open API service indexing awesome lists of open source software.

https://github.com/godeltech/godeltech.database.entityframeworkcore

Library to work with database, apply migrations and data using Entity Framework Core
https://github.com/godeltech/godeltech.database.entityframeworkcore

Last synced: 24 days ago
JSON representation

Library to work with database, apply migrations and data using Entity Framework Core

Awesome Lists containing this project

README

        

# GodelTech.Database.EntityFrameworkCore

# Description
GodelTech.Database.EntityFrameworkCore is a .NET library designed to work with databases, apply migrations, and manage data using Entity Framework Core. It simplifies the process of creating console applications with database migrations and data operations.

## Overview

Using this library you can create console app with database migrations and data.

`DatabaseService.cs`
```csharp
public class DatabaseService : DatabaseServiceBase
{
public DatabaseService(
BaseContext baseContext,
WeatherContext weatherContext,
IHostEnvironment hostEnvironment,
ILogger logger)
: base(logger, baseContext, weatherContext)
{
RegisterDataService(
new DataService(
new ConfigurationBuilder(),
hostEnvironment,
@"Data\Weather",
weatherContext,
false,
x => x.Id,
logger
)
);
}
}
```

`Worker.cs`
```csharp
public class Worker : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger _logger;

public Worker(IServiceProvider serviceProvider, ILogger logger)
{
_serviceProvider = serviceProvider;
_logger = logger;
}

private static readonly Action LogExecuteAsyncApplyDatabaseMigrationsInformation =
LoggerMessage.Define(
LogLevel.Information,
new EventId(0, nameof(ExecuteAsync)),
"Apply database migrations"
);

private static readonly Action LogExecuteAsyncApplyDatabaseDataInformation =
LoggerMessage.Define(
LogLevel.Information,
new EventId(0, nameof(ExecuteAsync)),
"Apply database data"
);

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = _serviceProvider.CreateScope();
var databaseService = scope.ServiceProvider.GetRequiredService();

LogExecuteAsyncApplyDatabaseMigrationsInformation(_logger, null);
await databaseService.ApplyMigrationsAsync();

LogExecuteAsyncApplyDatabaseDataInformation(_logger, null);
await databaseService.ApplyDataAsync();
}
}
```

# License
This project is licensed under the MIT License. See the LICENSE file for more details.