public void EditSighting(Sighting sighting) { sighting.Ispublished = true; using (var db = new SuperheroDBContext()) { Sighting toEdit = db.Sightings.Include("SightingHeroes").SingleOrDefault(s => s.SightingID == sighting.SightingID); if (toEdit != null) { toEdit.SightingDescription = sighting.SightingDescription; toEdit.Date = sighting.Date; toEdit.SightingLocation = db.Locations.Single(l => l.LocationID == sighting.SightingLocation.LocationID); var heroesToDelete = new List <Hero>(); foreach (var hero in toEdit.SightingHeroes) { heroesToDelete.Add(hero); } foreach (var hero in heroesToDelete) { toEdit.SightingHeroes.Remove(hero); } foreach (Hero hero in sighting.SightingHeroes) { toEdit.SightingHeroes.Add(db.Heroes.Single(h => h.HeroID == hero.HeroID)); } db.SaveChanges(); } } }
public void EditHero(Hero HeroID) { using (var db = new SuperheroDBContext()) { Hero toEdit = db.Heroes.Include("Organizations").SingleOrDefault(h => h.HeroID == HeroID.HeroID); if (toEdit != null) { toEdit.Description = HeroID.Description; toEdit.HeroName = HeroID.HeroName; toEdit.Sightings = HeroID.Sightings; toEdit.Superpower = HeroID.Superpower; var orgsToDelete = new List <Organization>(); foreach (var org in toEdit.Organizations) { orgsToDelete.Add(org); } foreach (var org in orgsToDelete) { toEdit.Organizations.Remove(org); } foreach (var org in HeroID.Organizations) { //db.Organizations.Add(org); toEdit.Organizations.Add(db.Organizations.Single(o => o.OrganizationID == org.OrganizationID)); } db.SaveChanges(); } } }
public void AddLocation(Location location) { using (var db = new SuperheroDBContext()) { db.Locations.Add(location); db.SaveChanges(); } }
//SuperheroDBContext context = new SuperheroDBContext(); public void AddHero(Hero hero) { using (var db = new SuperheroDBContext()) { //db.Set<Hero>().AddOrUpdate(hero); db.Heroes.Add(hero); db.SaveChanges(); } }
public void DeleteLocation(int LocationID) { using (var db = new SuperheroDBContext()) { var orgsToRemove = db.Organizations.Where(o => o.OrganizationLocation.LocationID == LocationID); foreach (var org in orgsToRemove) { db.Organizations.Remove(org); } db.SaveChanges(); Location toRemove = db.Locations.SingleOrDefault(l => l.LocationID == LocationID); if (toRemove != null) { db.Locations.Remove(toRemove); } db.SaveChanges(); } }
public void DeleteHero(int HeroID) { using (var db = new SuperheroDBContext()) { Hero toRemove = db.Heroes.SingleOrDefault(h => h.HeroID == HeroID); if (toRemove != null) { db.Heroes.Remove(toRemove); } db.SaveChanges(); } }
public void DeleteOrganization(int OrganizationID) { using (var db = new SuperheroDBContext()) { Organization toRemove = db.Organizations.SingleOrDefault(o => o.OrganizationID == OrganizationID); if (toRemove != null) { db.Organizations.Remove(toRemove); } db.SaveChanges(); } }
public void DeleteSighting(int sightingID) { using (var db = new SuperheroDBContext()) { // might be an issue here with capitalization Sighting toRemove = db.Sightings.SingleOrDefault(s => s.SightingID == sightingID); if (toRemove != null) { db.Sightings.Remove(toRemove); } db.SaveChanges(); } }
public void AddOrganization(Organization organization) { using (var db = new SuperheroDBContext()) { organization.OrganizationHeroes.Clear(); organization.OrganizationLocation = db.Locations.FirstOrDefault(l => l.LocationID == organization.OrganizationLocation.LocationID); foreach (var heroID in organization.SelectedHeroesID) { organization.OrganizationHeroes.Add(db.Heroes.Single(h => h.HeroID == heroID)); } db.Organizations.Add(organization); db.SaveChanges(); } }
public void EditOrg(Organization OrganizationID) { using (var db = new SuperheroDBContext()) { Organization toEdit = db.Organizations.Include("OrganizationHeroes").SingleOrDefault(o => o.OrganizationID == OrganizationID.OrganizationID); if (toEdit != null) { toEdit.OganizationAddress = OrganizationID.OganizationAddress; toEdit.OrganizationLocation = db.Locations.Single(l => l.LocationID == OrganizationID.OrganizationLocation.LocationID); toEdit.OrganizationName = OrganizationID.OrganizationName; toEdit.Phone = OrganizationID.Phone; toEdit.OrganizationHeroes.Clear(); db.SaveChanges(); foreach (Hero hero in OrganizationID.OrganizationHeroes) { //db.Heroes.Attach(hero); toEdit.OrganizationHeroes.Add(db.Heroes.Single(h => h.HeroID == hero.HeroID)); } db.SaveChanges(); } } }
public void AddSighting(Sighting sighting) { sighting.Ispublished = true; using (var db = new SuperheroDBContext()) { sighting.SightingLocation = db.Locations.FirstOrDefault(l => l.LocationID == sighting.SightingLocation.LocationID); sighting.SightingHeroes = new List <Hero>(); foreach (var HeroID in sighting.SelectedHeroesID) { sighting.SightingHeroes.Add(db.Heroes.First(h => h.HeroID == HeroID)); } db.Sightings.Add(sighting); db.SaveChanges(); } }
public void EditLocation(Location LocationID) { using (var db = new SuperheroDBContext()) { Location toEdit = db.Locations.SingleOrDefault(l => l.LocationID == LocationID.LocationID); if (toEdit != null) { toEdit.LatitudeCoordinate = LocationID.LatitudeCoordinate; toEdit.LongitudeCoordinate = LocationID.LongitudeCoordinate; toEdit.LocationAddress = LocationID.LocationAddress; toEdit.LocationDescription = LocationID.LocationDescription; toEdit.LocationName = LocationID.LocationName; db.SaveChanges(); } } }