public static void EnsureSeedForContext(this LotterySyndicateContext context) { if (context.LotteryTickets.Any()) { return; } var startDate = new DateTime(2018, 02, 06); var lotteryTickets = new List <LotteryTicket>(); //generate some lottery lines! for (int i = 0; i < 3; i++) { var numberOfLines = rand.Next(1, 5); var seedLotteryLinesSet = new List <LotteryLine>(); while (seedLotteryLinesSet.Count < numberOfLines) { seedLotteryLinesSet.Add(new LotteryLine() { LotteryNumbers = GenerateNumbers() }); } var ticketToAdd = new LotteryTicket() { DrawDate = startDate, LotteryLines = seedLotteryLinesSet }; lotteryTickets.Add(ticketToAdd); startDate = startDate.AddDays(-7); } context.LotteryTickets.AddRange(lotteryTickets); context.SaveChanges(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LotterySyndicateContext lotterySyndicateContext) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); lotterySyndicateContext.EnsureSeedForContext(); AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.LotteryTicket, Models.LotteryTicketDto>(); cfg.CreateMap <Entities.LotteryLine, Models.LotteryLineDto>(); cfg.CreateMap <Entities.LotteryNumber, Models.LotteryNumberDto>(); cfg.CreateMap <Models.LotteryTicketForCreationDto, Entities.LotteryTicket>(); }); app.UseMvc(); }
public LotterySyndicateRepository(LotterySyndicateContext context) { _context = context; }