public static int GetPostalCodeId(ApplicationDbContext _context, Postalcode postalCode) { var result = _context.postalcode .Where(p => p.Code == postalCode.Code && p.City.Name == postalCode.City.Name && p.City.State.Name == postalCode.City.State.Name).SingleOrDefault(); return(result == null ? 0 : result.CityId); }
public static Postalcode AddPostalCode(ApplicationDbContext _context, Postalcode postalCode) { var newPostalCode = new Postalcode() { Code = postalCode.Code, CityId = City.GetCityId(_context, postalCode.City) }; if (newPostalCode.CityId == 0) { newPostalCode.City = City.AddCity(_context, postalCode.City); newPostalCode.CityId = newPostalCode.City.Id; } _context.postalcode.Add(newPostalCode); _context.SaveChanges(); return(newPostalCode); }
public static Address AddAddress(ApplicationDbContext _context, Address address) { var newAddress = new Address() { AddressLine = address.AddressLine, PostalCodeId = Postalcode.GetPostalCodeId(_context, address.PostalCode) }; if (newAddress.PostalCodeId == 0) { newAddress.PostalCode = Postalcode.AddPostalCode(_context, address.PostalCode); newAddress.PostalCodeId = newAddress.PostalCode.CityId; } _context.Addresses.Add(newAddress); _context.SaveChanges(); return(newAddress); }