Skip to content

Commit

Permalink
Merge pull request #4 from kimcu-on-thenet/feature/efcore-migration-h…
Browse files Browse the repository at this point in the history
…ostedservice

Feature/efcore migration hostedservice
  • Loading branch information
thangchung authored Mar 5, 2020
2 parents 770de05 + 77170c8 commit 961cf12
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@ $ docker-compose -f docker-compose.yml -f docker-compose.override.yml run sqlser

## Step 2

```bash
$ cd src\ProductCatalog\CoolStore.ProductCatalogApi
$ dotnet ef database update
```

## Step 3

```bash
$ dotnet run -p \src\GraphApi\CoolStore.GraphApi\CoolStore.GraphApi.csproj
$ dotnet run -p \src\ProductCatalog\CoolStore.ProductCatalogApi\CoolStore.ProductCatalogApi.csproj
```

## Step 4
## Step 3
Go to http://localhost:5000

```js
products(currentPage: 1, highPrice: 1000) {
id
name
imageUrl
query{
products(currentPage: 1, highPrice: 1000) {
id
name
imageUrl
}
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace N8T.Infrastructure.Data
{
public class DbContextMigratorHostedService : IHostedService
{
private readonly IServiceProvider _serviceProvider;
public DbContextMigratorHostedService(IServiceProvider serviceProvider)
=> this._serviceProvider = serviceProvider;

#region Implementation of IHostedService

public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = this._serviceProvider.CreateScope();

var applicationDbContext = scope.ServiceProvider.GetRequiredService<DbContext>();

await applicationDbContext.Database.MigrateAsync(cancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="HostedServices\" />
<Folder Include="Persistence\Migrations\" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/ProductCatalog/CoolStore.ProductCatalogApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using N8T.Infrastructure.Options;
using Serilog;
using System.Threading.Tasks;
using N8T.Infrastructure.Data;

namespace CoolStore.ProductCatalogApi
{
Expand Down Expand Up @@ -59,6 +60,7 @@ private static async Task Main(string[] args)
});
builder.Services.AddScoped<DbContext>(provider => provider.GetService<ProductCatalogDbContext>());
builder.Services.AddScoped<IDomainEventContext>(provider => provider.GetService<ProductCatalogDbContext>());
builder.Services.AddHostedService<DbContextMigratorHostedService>();

var app = builder.Build();
app.Listen(serviceOptions.ProductCatalogService.RestUri);
Expand Down

0 comments on commit 961cf12

Please sign in to comment.