public static void EnsureSeedDataForContext(this CategoryInfoContext context) { if (context.Categories.Any()) { return; } var categories = new List <Category>() { new Category() { Name = "Basket", Description = "The sport where Lebron is the king." }, new Category() { Name = "Football", Description = "The sport where Christiano Ronaldo is a winner." }, new Category() { Name = "Boxing", Description = "The sport where Tyson was the best" } }; context.Categories.AddRange(categories); 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, CategoryInfoContext categoryInfoContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } categoryInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.Category, Models.CategoryDto>(); }); app.UseMvc(); }
public CategoryInfoRepository(CategoryInfoContext context) { _context = context; }