Revert "make sure that foodplaces and relations don't appear twice in the DB"

This reverts commit 382b9be68a.
This commit is contained in:
Andreas Zweili 2018-09-02 15:58:30 +02:00
parent 6e9f708af7
commit e477b25be1
2 changed files with 29 additions and 49 deletions

View File

@ -24,29 +24,18 @@ namespace Server.DB
{ {
using (Context ctx = new Context()) using (Context ctx = new Context())
{ {
var foodplaceFromDB = ctx.FoodPlaces.SingleOrDefault(x => x.Name == foodplace.Name var city = ctx.Cities.FirstOrDefault(c => c.CityID == foodplace.City.CityID);
&& x.Streetname == foodplace.Streetname var country = ctx.Countries.FirstOrDefault(c => c.CountryID == foodplace.City.Country.CountryID);
&& x.Streetnumber == foodplace.Streetnumber
&& x.City.CityID == foodplace.City.CityID);
if (foodplaceFromDB != null)
{
return true;
}
else
{
var city = ctx.Cities.FirstOrDefault(c => c.CityID == foodplace.City.CityID);
var country = ctx.Countries.FirstOrDefault(c => c.CountryID == foodplace.City.Country.CountryID);
ctx.Cities.Attach(city); ctx.Cities.Attach(city);
ctx.Countries.Attach(country); ctx.Countries.Attach(country);
foodplace.City = city; foodplace.City = city;
ctx.FoodPlaces.Add(foodplace); ctx.FoodPlaces.Add(foodplace);
ctx.SaveChanges(); ctx.SaveChanges();
return true;
}
} }
return true;
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -26,40 +26,31 @@ namespace Server.DB
{ {
using (Context ctx = new Context()) using (Context ctx = new Context())
{ {
var relation = ctx.PatientAtFoodPlaces.SingleOrDefault(x => x.Patient.PersonID == patientAtFoodPlace.Patient.PersonID var patient = ctx.Persons.FirstOrDefault(p => p.PersonID == patientAtFoodPlace.Patient.PersonID);
&& x.FoodPlace.FoodPlaceID == patientAtFoodPlace.FoodPlace.FoodPlaceID); var foodplace = ctx.FoodPlaces.FirstOrDefault(f => f.FoodPlaceID == patientAtFoodPlace.FoodPlace.FoodPlaceID);
if (relation != null) var pcity = ctx.Cities.FirstOrDefault(c => c.CityID == patient.City.CityID);
{ var fcity = ctx.Cities.FirstOrDefault(c => c.CityID == patientAtFoodPlace.FoodPlace.City.CityID);
return true; var pcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == pcity.Country.CountryID);
} var fcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == fcity.Country.CountryID);
else var salutation = ctx.Salutations.FirstOrDefault(s => s.SalutationID == patient.Salutation.SalutationID);
{ var gender = ctx.Genders.FirstOrDefault(g => g.GenderID == patient.Gender.GenderID);
var patient = ctx.Persons.FirstOrDefault(p => p.PersonID == patientAtFoodPlace.Patient.PersonID);
var foodplace = ctx.FoodPlaces.FirstOrDefault(f => f.FoodPlaceID == patientAtFoodPlace.FoodPlace.FoodPlaceID);
var pcity = ctx.Cities.FirstOrDefault(c => c.CityID == patient.City.CityID);
var fcity = ctx.Cities.FirstOrDefault(c => c.CityID == patientAtFoodPlace.FoodPlace.City.CityID);
var pcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == pcity.Country.CountryID);
var fcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == fcity.Country.CountryID);
var salutation = ctx.Salutations.FirstOrDefault(s => s.SalutationID == patient.Salutation.SalutationID);
var gender = ctx.Genders.FirstOrDefault(g => g.GenderID == patient.Gender.GenderID);
ctx.Cities.Attach(pcity); ctx.Cities.Attach(pcity);
ctx.Cities.Attach(fcity); ctx.Cities.Attach(fcity);
ctx.Countries.Attach(pcountry); ctx.Countries.Attach(pcountry);
ctx.Countries.Attach(fcountry); ctx.Countries.Attach(fcountry);
ctx.Genders.Attach(gender); ctx.Genders.Attach(gender);
ctx.Salutations.Attach(salutation); ctx.Salutations.Attach(salutation);
ctx.Persons.Attach(patient); ctx.Persons.Attach(patient);
ctx.FoodPlaces.Attach(foodplace); ctx.FoodPlaces.Attach(foodplace);
patientAtFoodPlace.FoodPlace = foodplace; patientAtFoodPlace.FoodPlace = foodplace;
patientAtFoodPlace.Patient = patient; patientAtFoodPlace.Patient = patient;
ctx.PatientAtFoodPlaces.Add(patientAtFoodPlace); ctx.PatientAtFoodPlaces.Add(patientAtFoodPlace);
ctx.SaveChanges(); ctx.SaveChanges();
return true;
}
} }
return true;
} }
catch (Exception e) catch (Exception e)
{ {