public void CreateFacility(int locationId, ref Facility facility)
        {
            //Save the facility record
            var insertedRowId = _da.Value.Facility.Insert(facility);

            if (insertedRowId == 0)
            {
                throw new Exception("Failed to create facility record");
            }
            else
            {
                facility.Id = insertedRowId;
            }

            //Link the facility and location records
            var locationFacilityLink = new LinkObjectMaster()
            {
                MasterLinkId = locationId,
                MasterLinkType = LinkType.Location,
                ChildLinkId = facility.Id,
                ChildLinkType = LinkType.Facility
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(locationFacilityLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the facility record
                _da.Value.Facility.Delete(facility);

                throw new Exception("Failed to create location facility link record, transaction rolled back");
            }
        }
        public void Delete(Facility deleteThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@Id", deleteThis.Id.ToString());

            _sqlToExecute = "DELETE FROM [dbo].[Facility] WHERE Id = " + _dataEngine.GetParametersForQuery();

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Facility - Delete failed");
        }
 public int Insert(Facility saveThis)
 {
     try
        {
        _db.Facilities.InsertOnSubmit(saveThis);
        _db.SubmitChanges();
        return saveThis.Id;
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
 }
 public void Delete(Facility deleteThis)
 {
     try
     {
         if (deleteThis.Id > 0)
         {
             _db.Facilities.DeleteOnSubmit(deleteThis);
             _db.SubmitChanges(ConflictMode.FailOnFirstConflict);
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
        public int Insert(Facility saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@FacilityBookAheadDays", saveThis.FacilityBookAheadDays.ToString());

            _sqlToExecute = "INSERT INTO [dbo].[Facility] ";
            _sqlToExecute += "([FacilityBookAheadDays]) ";
            _sqlToExecute += "OUTPUT INSERTED.Id ";
            _sqlToExecute += "VALUES ";
            _sqlToExecute += "(";
            _sqlToExecute += _dataEngine.GetParametersForQuery();
            _sqlToExecute += ")";

            int insertedRowId = 0;

            if (!_dataEngine.ExecuteSql(_sqlToExecute, out insertedRowId))
                throw new Exception("Facility - Save failed");

            return insertedRowId;
        }
        /// <summary>
        /// Creates the object from the data returned from the database
        /// </summary>
        /// <returns></returns>
        private Facility CreateFacilityFromData()
        {
            var facility = new Facility
            {
                Id = int.Parse(_dataEngine.Dr["Id"].ToString()),
                FacilityBookAheadDays = int.Parse(_dataEngine.Dr["FacilityBookAheadDays"].ToString())
            };

            return facility;
        }
        public void Update(Facility saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@FacilityBookAheadDays", saveThis.FacilityBookAheadDays.ToString());

            _sqlToExecute = "UPDATE [dbo].[Facility] SET ";
            _sqlToExecute += "[FacilityBookAheadDays] = @FacilityBookAheadDays ";
            _sqlToExecute += "WHERE [Id] = " + saveThis.Id;

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Facility - Update failed");
        }
 public void Update(Facility updateThis)
 {
     try
     {
         _db.Facilities.Attach(updateThis);
         _db.Refresh(RefreshMode.KeepCurrentValues, updateThis);
         _db.SubmitChanges();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }