edit personDB to fit EF rules
This commit is contained in:
parent
b7668dd276
commit
94db018d32
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue