edit personDB to fit EF rules

This commit is contained in:
Strati 2018-08-05 22:00:49 +02:00
parent b7668dd276
commit 94db018d32
2 changed files with 19 additions and 8 deletions

View File

@ -101,14 +101,9 @@ namespace Server.DB
.HasRequired(p => p.Gender);
modelBuilder.Entity<Person>()
.HasRequired(p => p.City)
.HasRequired(p => p.City)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Person>()
.HasMany<PatientAtFoodPlace>(p => p.PatientAtFoodPlaces)
.WithRequired()
.HasForeignKey(p => p.PatientID);
.WillCascadeOnDelete(false);
modelBuilder.Entity<FoodPlace>()
.Property(f => f.Name)

View File

@ -21,11 +21,27 @@ namespace Server.DB
}
}
public bool CreatePerson(Person person)
{
{
try
{
using (Context ctx = new Context())
{
var city = ctx.Cities.FirstOrDefault(c => c.CityID == person.City.CityID);
var salutation = ctx.Salutations.FirstOrDefault(c => c.SalutationID == person.Salutation.SalutationID);
var gender = ctx.Genders.FirstOrDefault(c => c.GenderID == person.Gender.GenderID);
var country = ctx.Countries.FirstOrDefault(c => c.CountryID == city.Country.CountryID);
ctx.Cities.Attach(city);
ctx.Salutations.Attach(salutation);
ctx.Genders.Attach(gender);
ctx.Countries.Attach(country);
person.City = city;
person.Salutation = salutation;
person.Gender = gender;
ctx.Persons.Add(person);
ctx.SaveChanges();
}