feat: add debugging class to populate the database
This commit is contained in:
parent
7904ba4391
commit
6ab1f13842
96
TicketOffice/Models/SeedData.cs
Normal file
96
TicketOffice/Models/SeedData.cs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using TicketOffice.Data;
|
||||||
|
|
||||||
|
namespace TicketOffice.Models;
|
||||||
|
|
||||||
|
public class SeedData
|
||||||
|
{
|
||||||
|
public static void Initialize(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
using var context =
|
||||||
|
new TicketOfficeContext(serviceProvider.GetRequiredService<DbContextOptions<TicketOfficeContext>>());
|
||||||
|
|
||||||
|
if (context == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("Null TicketOfficeContext");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.User.Any() | context.Route.Any() | context.City.Any() | context.Ticket.Any())
|
||||||
|
{
|
||||||
|
return; // Data has been seeded
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
|
||||||
|
context.User.AddRange(new User[]
|
||||||
|
{
|
||||||
|
new User
|
||||||
|
{
|
||||||
|
Email = "danylo.nazarko@nure.ua",
|
||||||
|
Password = "*Hashed Password*",
|
||||||
|
IsManager = false,
|
||||||
|
},
|
||||||
|
new User
|
||||||
|
{
|
||||||
|
Email = "ruslan.shanin@nure.ua",
|
||||||
|
Password = "*Hashed Password*",
|
||||||
|
IsManager = false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
context.Route.AddRange(new Route[]
|
||||||
|
{
|
||||||
|
new Route {
|
||||||
|
Number = "0001",
|
||||||
|
Capacity = 30,
|
||||||
|
Cities = new City[]
|
||||||
|
{
|
||||||
|
new City
|
||||||
|
{
|
||||||
|
Name = "Кремінна",
|
||||||
|
ArrivalTime = new DateTime(2022, 03, 28, 8, 15, 0),
|
||||||
|
DepartureTime = new DateTime(2022, 03, 28, 8, 35, 0),
|
||||||
|
},
|
||||||
|
new City
|
||||||
|
{
|
||||||
|
Name = "Рубіжне",
|
||||||
|
ArrivalTime = new DateTime(2022, 03, 28, 9, 5, 0),
|
||||||
|
DepartureTime = new DateTime(2022, 03, 28, 9, 25, 0),
|
||||||
|
},
|
||||||
|
new City
|
||||||
|
{
|
||||||
|
Name = "Сєвєродонецьк",
|
||||||
|
ArrivalTime = new DateTime(2022, 03, 28, 9, 55, 0)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
new Route
|
||||||
|
{
|
||||||
|
Number = "0002",
|
||||||
|
Capacity = 25,
|
||||||
|
Cities = new City[]
|
||||||
|
{
|
||||||
|
new City
|
||||||
|
{
|
||||||
|
Name = "Сєвєродонецьк",
|
||||||
|
ArrivalTime = new DateTime(2022, 03, 28, 15, 55, 0),
|
||||||
|
DepartureTime = new DateTime(2022, 03, 28, 16, 15, 0),
|
||||||
|
},
|
||||||
|
new City
|
||||||
|
{
|
||||||
|
Name = "Рубіжне",
|
||||||
|
ArrivalTime = new DateTime(2022, 03, 28, 16, 45, 0),
|
||||||
|
DepartureTime = new DateTime(2022, 03, 28, 17, 5, 0),
|
||||||
|
},
|
||||||
|
new City
|
||||||
|
{
|
||||||
|
Name = "Кремінна",
|
||||||
|
ArrivalTime = new DateTime(2022, 03, 28, 17, 40, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using TicketOffice.Data;
|
using TicketOffice.Data;
|
||||||
|
using TicketOffice.Models;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@ -12,6 +13,10 @@ builder.Services.AddDbContext<TicketOfficeContext>(options =>
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
using var scope = app.Services.CreateScope();
|
||||||
|
var services = scope.ServiceProvider;
|
||||||
|
SeedData.Initialize(services);
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (!app.Environment.IsDevelopment())
|
if (!app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user