public Owner GetOwner(int id)
 {
     using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
     {
         var dbOwner = entities.OWNERs.Include("LAND_PROPERTY").FirstOrDefault(o => o.ID == id);
         return ConvertDbToBusinessEntity(dbOwner);
     }
 }
 public void Delete(int id)
 {
     using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
     {
         var mortgage = entities.MORTGAGEs.FirstOrDefault(m => m.ID == id);
         entities.MORTGAGEs.Remove(mortgage);
         entities.SaveChanges();
     }
 }
 public void Delete(int id)
 {
     using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
     {
         var ownerToDelete = entities.OWNERs.FirstOrDefault(o => o.ID == id);
         entities.OWNERs.Remove(ownerToDelete);
         entities.SaveChanges();
     }
 }
 public void Add(LandProperty landProperty)
 {
     using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
     {
         LAND_PROPERTY dbObjectToAdd = new LAND_PROPERTY();
         CopyDataFromBusinessToEFObject(dbObjectToAdd, landProperty);
         entities.LAND_PROPERTY.Add(dbObjectToAdd);
         entities.SaveChanges();
     }
 }
        public List<Owner> GetAllOwners()
        {
            List<Owner> result = new List<Owner>();
            using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
            {
                var dbEntities = entities.OWNERs.ToList();
                dbEntities.ForEach(dbEntity => result.Add(ConvertDbToBusinessEntity(dbEntity)));
            }

            return result;
        }
        public LandProperty GetLandProperty(int id)
        {
            LandProperty result = null;

            using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
            {
                var dbLandProperty = entities.LAND_PROPERTY.Include("MORTGAGEs").Include("OWNER").FirstOrDefault(lp => lp.ID == id);
                result = ConvertToBusinessEntity(dbLandProperty);
            }

            return result;
        }
        public void Edit(LandProperty landProperty)
        {
            using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
            {
                var dbLandProperty = entities.LAND_PROPERTY.Include("OWNER").Include("MORTGAGEs").FirstOrDefault(lp => lp.ID == landProperty.ID);

                dbLandProperty.MORTGAGEs.ToList().ForEach(m => entities.MORTGAGEs.Remove(m));
                CopyDataFromBusinessToEFObject(dbLandProperty, landProperty);

                entities.SaveChanges();
            }
        }
        public Owner EditOwner(Owner owner)
        {
            using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
            {
                var dbOwner = entities.OWNERs.FirstOrDefault(o => o.ID == owner.ID);
                dbOwner.FIRST_NAME = owner.FirstName;
                dbOwner.LAST_NAME = owner.LastName;
                dbOwner.ADDRESS = owner.Address;

                entities.SaveChanges();
                return ConvertDbToBusinessEntity(dbOwner);
            }
        }
        public List<LandProperty> GetAllLandProperties()
        {
            List<LandProperty> result = new List<LandProperty>();
            using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
            {
                var landProperties = entities.LAND_PROPERTY.Include("MORTGAGEs").Include("OWNER").ToList();
                foreach (var dbLandProperty in landProperties)
                {
                    result.Add(ConvertToBusinessEntity(dbLandProperty));
                }
            }

            return result;
        }
        public void Delete(int id)
        {
            using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
            {
                //Delete mortgages associates to this land property
                var mortgagesToThisProperty = entities.MORTGAGEs.Where(m => m.LAND_PROPERTY_ID == id).ToList();
                mortgagesToThisProperty.ForEach(m => entities.MORTGAGEs.Remove(m));

                //Delete the land property
                var landPropertyToDelete = entities.LAND_PROPERTY.FirstOrDefault(lp => lp.ID == id);
                entities.LAND_PROPERTY.Remove(landPropertyToDelete);

                entities.SaveChanges();
            }
        }
        public Owner AddOwner(Owner owner)
        {
            Owner addedOwner = null;
            using (LandPropertiesDBEntities entities = new LandPropertiesDBEntities())
            {
                OWNER ownerToAdd = new OWNER
                                    {
                                        FIRST_NAME = owner.FirstName,
                                        LAST_NAME = owner.LastName,
                                        ADDRESS = owner.Address
                                    };

                entities.OWNERs.Add(ownerToAdd);
                entities.SaveChanges();
                addedOwner = ConvertDbToBusinessEntity(ownerToAdd);
            }

            return addedOwner;
        }