public void AddOrUpdate(object entity) { var state = _ctx.Entry(entity).State; switch (state) { case EntityState.Detached: _ctx.Add(entity); break; case EntityState.Modified: _ctx.Update(entity); break; case EntityState.Added: case EntityState.Deleted: case EntityState.Unchanged: // do nothing break; } }
public async Task SeedAsync() { try { if (_env.IsDevelopment()) { if (_config["Data:IterateDatabase"].ToLower() == "true") { await _ctx.Database.EnsureDeletedAsync(); await _ctx.Database.EnsureCreatedAsync(); } var admin = await _userManager.FindByEmailAsync(_config["Admin:SuperUser:Email"]); // If no Admin, then we haven't seeded the database if (admin == null) { admin = new CodeCampUser() { UserName = _config["Admin:SuperUser:Email"], Email = _config["Admin:SuperUser:Email"], Name = _config["Admin:SuperUser:Name"], EmailConfirmed = true }; // Create Super User if (!(await _userManager.CreateAsync(admin, _config["Admin:SuperUser:TempPassword"])).Succeeded) { throw new InvalidOperationException("Failed to create Super User"); } if (!(await _roleManager.CreateAsync(new IdentityRole(Consts.ADMINROLE))).Succeeded) { throw new InvalidOperationException("Failed to create Admin Role"); } // Add to Admin Role if (!(await _userManager.AddToRoleAsync(admin, Consts.ADMINROLE)).Succeeded) { throw new InvalidOperationException("Failed to update Super User Role"); } } EventInfo[] codeCamps; if (!_ctx.CodeCampEvents.Any()) { codeCamps = new EventInfo[] { new EventInfo() { Moniker = "2017", Name = "Atlanta Code Camp 2017", EventDate = new DateTime(2017, 9, 17), EventLength = 1, Description = "The Atlanta Code Camp is awesome! The Atlanta Code Camp is awesome! The Atlanta Code Camp is awesome!", IsDefault = true, TwitterLink = "https://twitter.com/atlcodecamp", ContactEmail = "*****@*****.**", CallForSpeakersOpened = new DateTime(2017, 5, 25), CallForSpeakersClosed = new DateTime(2016, 9, 1), Location = new EventLocation() { Facility = "Kennesaw State University", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2016", Name = "Atlanta Code Camp 2016", EventDate = new DateTime(2016, 10, 15), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = true, TwitterLink = "https://twitter.com/atlcodecamp", ContactEmail = "*****@*****.**", CallForSpeakersOpened = new DateTime(2016, 7, 1), CallForSpeakersClosed = new DateTime(2016, 10, 1), Location = new EventLocation() { Facility = "Kennesaw State University", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2015", Name = "Atlanta Code Camp 2015", EventDate = new DateTime(2015, 10, 24), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = false, CallForSpeakersOpened = new DateTime(2015, 8, 1), CallForSpeakersClosed = new DateTime(2015, 10, 1), Location = new EventLocation() { Facility = "Kennesaw State University (Formerly Southern Polytechnic)", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2014", Name = "Atlanta Code Camp 2014", EventDate = new DateTime(2014, 10, 11), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = false, CallForSpeakersOpened = new DateTime(2014, 8, 1), CallForSpeakersClosed = new DateTime(2014, 9, 15), Location = new EventLocation() { Facility = "Southern Polytechnic", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } }, new EventInfo() { Moniker = "2013", Name = "Atlanta Code Camp 2013", EventDate = new DateTime(2013, 8, 24), EventLength = 1, Description = "The Atlanta Code Camp is awesome", IsDefault = false, CallForSpeakersOpened = new DateTime(2013, 6, 1), CallForSpeakersClosed = new DateTime(2013, 8, 1), Location = new EventLocation() { Facility = "Southern Polytechnic", Address1 = "1100 S Marietta Pkwy", Address2 = "", City = "Marietta", StateProvince = "GA", PostalCode = "30060", Country = "USA", Link = "" } } }; _ctx.AddRange(codeCamps); await _ctx.SaveChangesAsync(); } else { codeCamps = _ctx.CodeCampEvents.ToArray(); } if (!_ctx.Sponsors.Any()) { var sponsor = new Sponsor() { Name = "Wilder Minds", Link = "http://wilderminds.com", Event = codeCamps[0], Paid = true, ImageUrl = "/img/2016/sponsors/wilder-minds.jpg", SponsorLevel = "Silver" }; _ctx.Add(sponsor); sponsor = new Sponsor() { Name = "Wilder Minds", Link = "http://wilderminds.com", Event = codeCamps[1], Paid = true, ImageUrl = "/img/2016/sponsors/wilder-minds.jpg", SponsorLevel = "Silver" }; _ctx.Add(sponsor); _ctx.AddRange(Add2015Sponsors(codeCamps.Where(s => s.Moniker == "2015").First())); _ctx.AddRange(Add2014Sponsors(codeCamps.Where(s => s.Moniker == "2014").First())); _ctx.AddRange(Add2013Sponsors(codeCamps.Where(s => s.Moniker == "2013").First())); await _ctx.SaveChangesAsync(); } if (!_ctx.Speakers.Any()) { await Migrate(); } } } catch (Exception ex) { // TODO ex.ToString(); } }