public static void EnsurePopulated(IApplicationBuilder app) { AppIdentityDbContext context = app.ApplicationServices .GetRequiredService <AppIdentityDbContext>(); UserManager <AppUser> userManager = app.ApplicationServices .GetRequiredService <UserManager <AppUser> >(); context.Database.Migrate(); if (!context.Users.AnyAsync().Result) { AppUser joe = new AppUser { UserName = "******", Email = "*****@*****.**", }; AppUser alice = new AppUser { UserName = "******", Email = "*****@*****.**", }; AppUser bob = new AppUser { UserName = "******", Email = "*****@*****.**", }; userManager.CreateAsync(joe, "secret123"); userManager.CreateAsync(alice, "secret123"); userManager.CreateAsync(bob, "secret123"); } context.SaveChanges(); }
public void SaveProduct(Fruit product) { if (product.FruitID == 0) { context.Products.Add(product); } else { Fruit dbEntry = context.Products .FirstOrDefault(p => p.FruitID == product.FruitID); if (dbEntry != null) { dbEntry.Name = product.Name; } } context.SaveChanges(); }
public static void EnsurePopulated(IServiceProvider services) { AppIdentityDbContext context = services.GetRequiredService <AppIdentityDbContext>(); //context.Database.Migrate(); if (!context.Products.Any()) { context.Products.AddRange( new Fruit { Name = "Apple" }, new Fruit { Name = "Orange" }, new Fruit { Name = "Banana" }, new Fruit { Name = "Pineapples" } ); context.SaveChanges(); } }