-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathProgram.cs
35 lines (31 loc) · 1.21 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
global using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using DataSample;
using var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
var connectionString = context.Configuration.GetConnectionString("MySQLConnection");
if (connectionString is null)
{
throw new InvalidOperationException("MySQLConnection is not configured");
}
ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
services.AddDbContext<BooksContext>(options =>
{
options.UseMySql(connectionString, serverVersion);
});
services.AddScoped<Runner>();
})
.Build();
await using var scope = host.Services.CreateAsyncScope();
var runner = scope.ServiceProvider.GetRequiredService<Runner>();
await runner.CreateTheDatabaseAsync();
await runner.AddBookAsync("Professional C# and .NET", "Wrox Press");
await runner.AddBooksAsync();
await runner.ReadBooksAsync();
await runner.QueryBooksAsync();
await runner.UpdateBookAsync();
await runner.DeleteBooksAsync();
await runner.DeleteDatabaseAsync();