public static void MapToDB(ThisEntity fromEntity, DBEntity to)
        {
            //want to ensure fields from create are not wiped
            if (to.Id < 1)
            {
                if (IsValidDate(fromEntity.Created))
                {
                    to.Created = fromEntity.Created;
                }
                to.CreatedById = fromEntity.CreatedById;
            }

            to.Id = fromEntity.Id;
            //to.Name = fromEntity.Name;
            to.Description  = fromEntity.Description;
            to.CredentialId = fromEntity.ParentId;



            if (IsValidDate(fromEntity.LastUpdated))
            {
                to.LastUpdated = fromEntity.LastUpdated;
            }
            to.LastUpdatedById = fromEntity.LastUpdatedById;
        }
        public bool Delete(int Id, ref string statusMessage)
        {
            bool isValid = false;

            if (Id == 0)
            {
                statusMessage = "Error - missing an identifier for the ConditionProfile";
                return(false);
            }
            using (var context = new EntityContext())
            {
                DBEntity efEntity = context.SearchPendingReindex
                                    .SingleOrDefault(s => s.Id == Id);

                if (efEntity != null && efEntity.Id > 0)
                {
                    context.SearchPendingReindex.Remove(efEntity);
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        isValid = true;
                    }
                }
                else
                {
                    statusMessage = "Error - delete failed, as record was not found.";
                }
            }

            return(isValid);
        }
        /// <summary>
        /// SearchPendingReindexes Add
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="messages"></param>
        /// <returns></returns>
        private int Add(ThisEntity entity, ref List <String> messages)
        {
            DBEntity efEntity = new DBEntity();

            if (!IsValid(entity, ref messages))
            {
                return(0);
            }
            try
            {
                using (var context = new EntityContext())
                {
                    //check if a pending record exists
                    DBEntity exists = context.SearchPendingReindex
                                      .FirstOrDefault(s => s.EntityTypeId == entity.EntityTypeId && s.RecordId == entity.RecordId && s.StatusId == 1);
                    if (exists != null && exists.Id > 0)
                    {
                        //could ignore,or check for a change of request type of add or delete
                        if (exists.IsUpdateOrDeleteTypeId == entity.IsUpdateOrDeleteTypeId)
                        {
                            return(exists.Id);
                        }
                        //otherwise do an update?
                        Update(entity, ref messages);
                        return(exists.Id);
                    }

                    MapToDB(entity, efEntity);
                    efEntity.Created     = System.DateTime.Now;
                    efEntity.LastUpdated = System.DateTime.Now;

                    context.SearchPendingReindex.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        entity.Id = efEntity.Id;

                        return(efEntity.Id);
                    }
                    else
                    {
                        //?no info on error
                        messages.Add("Error - the profile was not saved. ");
                        string message = string.Format(thisClassName + ".Add. Failed. The process appeared to not work, but was not an exception, so we have no message, or no clue. EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId);
                        // EmailManager.NotifyAdmin( thisClassName + ". Add Failed", message );
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(). EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId));
            }


            return(efEntity.Id);
        }
        /// <summary>
        /// Update a ThisEntity
        /// - base only, caller will handle parts?
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="statusMessage"></param>
        /// <returns></returns>
        public bool Update(ThisEntity entity, ref string statusMessage)
        {
            bool isValid = false;
            int  count   = 0;

            try
            {
                using (var context = new EntityContext())
                {
                    if (!IsValid(entity, ref messages))
                    {
                        return(0);
                    }

                    DBEntity efEntity = context.Credential_ConnectionProfile
                                        .SingleOrDefault(s => s.Id == entity.Id);

                    if (efEntity != null && efEntity.Id > 0)
                    {
                        //for updates, chances are some fields will not be in interface, don't map these (ie created stuff)
                        MapToDB(entity, efEntity);
                        if (HasStateChanged(context))
                        {
                            efEntity.LastUpdated     = System.DateTime.Now;
                            efEntity.LastUpdatedById = entity.LastUpdatedById;
                            count = context.SaveChanges();
                            //can be zero if no data changed
                            if (count >= 0)
                            {
                                isValid = true;
                            }
                            else
                            {
                                //?no info on error
                                statusMessage = "Error - the update was not successful. ";
                                string message = string.Format(thisClassName + ".ConditionProfile_Update Failed", "Attempted to update a ThisEntity. The process appeared to not work, but was not an exception, so we have no message, or no clue. CredentialId: {0}, Id: {1}, updatedById: {2}", entity.ParentId, entity.Id, entity.LastUpdatedById);
                                EmailManager.NotifyAdmin(thisClassName + ". ConditionProfile_Update Failed", message);
                            }
                        }
                        //continue with parts regardless
                        //opMgr.ConditionProfile_UpdateParts( entity, false, ref statusMessage );
                    }
                    else
                    {
                        statusMessage = "Error - update failed, as record was not found.";
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + string.Format(".Update. id: {0}", entity.Id));
            }


            return(isValid);
        }
        /// <summary>
        /// Update a Record
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="messages"></param>
        /// <returns></returns>
        public bool Update(ThisEntity entity, ref List <String> messages)
        {
            bool isValid = false;
            int  count   = 0;

            try
            {
                if (!IsValid(entity, ref messages))
                {
                    return(false);
                }

                using (var context = new EntityContext())
                {
                    DBEntity efEntity = context.SearchPendingReindex
                                        .FirstOrDefault(s => s.Id == entity.Id);

                    if (efEntity != null && efEntity.Id > 0)
                    {
                        //for updates, chances are some fields will not be in interface, don't map these (ie created stuff)
                        MapToDB(entity, efEntity);
                        if (HasStateChanged(context))
                        {
                            efEntity.LastUpdated = System.DateTime.Now;

                            count = context.SaveChanges();
                            //can be zero if no data changed
                            if (count >= 0)
                            {
                                isValid = true;
                            }
                            else
                            {
                                //?no info on error
                                messages.Add("Error - the update was not successful. ");
                                string message = string.Format(thisClassName + ".Update Failed. The process appeared to not work, but was not an exception, so we have no message, or no clue. EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId);
                                //EmailManager.NotifyAdmin( thisClassName + ". ConditionProfile_Update Failed", message );
                            }
                        }
                    }
                    else
                    {
                        messages.Add("Error - update failed, as record was not found.");
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + string.Format(".Update. EntityTypeId: {0}, RecordId: {1}", entity.EntityTypeId, entity.RecordId));
            }


            return(isValid);
        }
        public static void MapToDB(ThisEntity fromEntity, DBEntity to)
        {
            //want to ensure fields from create are not wiped
            if (to.Id < 1)
            {
            }

            to.Id                     = fromEntity.Id;
            to.EntityTypeId           = fromEntity.EntityTypeId;
            to.RecordId               = fromEntity.RecordId;
            to.StatusId               = fromEntity.StatusId;
            to.IsUpdateOrDeleteTypeId = fromEntity.IsUpdateOrDeleteTypeId;
        }
        public static ThisEntity Get(int id, bool includeProperties = false)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                DBEntity item = context.SearchPendingReindex
                                .SingleOrDefault(s => s.Id == id);

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity, true);
                }
            }

            return(entity);
        }
        public static void MapFromDB(DBEntity fromEntity, ThisEntity to, bool includingProperties = false)
        {
            to.Id                     = fromEntity.Id;
            to.EntityTypeId           = fromEntity.EntityTypeId;
            to.RecordId               = fromEntity.RecordId;
            to.StatusId               = fromEntity.StatusId;
            to.IsUpdateOrDeleteTypeId = (int)fromEntity.IsUpdateOrDeleteTypeId;

            if (IsValidDate(fromEntity.Created))
            {
                to.Created = ( DateTime )fromEntity.Created;
            }

            if (IsValidDate(fromEntity.LastUpdated))
            {
                to.LastUpdated = ( DateTime )fromEntity.LastUpdated;
            }
        }
        public static void MapFromDB(DBEntity fromEntity, ThisEntity to, bool includingProperties = false)
        {
            to.Id          = fromEntity.Id;
            to.RowId       = fromEntity.RowId;
            to.ParentId    = fromEntity.CredentialId;
            to.ProfileName = fromEntity.Name;
            to.Description = fromEntity.Description;

            //....

            if (IsValidDate(fromEntity.Created))
            {
                to.Created = ( DateTime )fromEntity.Created;
            }
            to.CreatedById = fromEntity.CreatedById == null ? 0 : ( int )fromEntity.CreatedById;
            if (IsValidDate(fromEntity.LastUpdated))
            {
                to.LastUpdated = ( DateTime )fromEntity.LastUpdated;
            }
            to.LastUpdatedById = fromEntity.LastUpdatedById == null ? 0 : ( int )fromEntity.LastUpdatedById;
        }
        public static ThisEntity Get(int id, bool includeProperties = false)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                DBEntity item = context.Credential_ConnectionProfile
                                .SingleOrDefault(s => s.Id == id);

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity, true);
                    if (includeProperties)
                    {
                        //TBD
                    }
                }
            }

            return(entity);
        }
        /// <summary>
        /// add a ThisEntity
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="statusMessage"></param>
        /// <returns></returns>
        public int Add(ThisEntity entity, ref List <String> messages)
        {
            DBEntity efEntity = new DBEntity();

            entity.ParentId = credential.Id;
            using (var context = new EntityContext())
            {
                try
                {
                    if (!IsValid(entity, ref messages))
                    {
                        return(0);
                    }
                    MapToDB(entity, efEntity);

                    efEntity.CredentialId = credential.Id;
                    if (efEntity.RowId == null || efEntity.RowId.ToString() == DEFAULT_GUID)
                    {
                        efEntity.RowId = Guid.NewGuid();
                    }
                    efEntity.CreatedById     = credential.LastUpdatedById;
                    efEntity.Created         = System.DateTime.Now;
                    efEntity.LastUpdatedById = credential.LastUpdatedById;
                    efEntity.LastUpdated     = System.DateTime.Now;

                    context.Credential_ConnectionProfile.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        entity.Id = efEntity.Id;

                        //opMgr.ConditionProfile_UpdateParts( entity, true, ref statusMessage );

                        return(efEntity.Id);
                    }
                    else
                    {
                        //?no info on error
                        messages.Add("Error - the profile was not saved. ");
                        string message = string.Format("ConditionProfileManager. ConditionProfile_Add Failed", "Attempted to add a ThisEntity. The process appeared to not work, but was not an exception, so we have no message, or no clue.ThisEntity. CredentialId: {0}, createdById: {1}", entity.ParentId, entity.CreatedById);
                        EmailManager.NotifyAdmin(thisClassName + ". ConditionProfile_Add Failed", message);
                    }
                }
                catch (System.Data.ThisEntity.Validation.DbEntityValidationException dbex)
                {
                    //LoggingHelper.LogError( dbex, thisClassName + string.Format( ".ContentAdd() DbEntityValidationException, Type:{0}", entity.TypeId ) );
                    string message = thisClassName + string.Format(".ConditionProfile_Add() DbEntityValidationException, CredentialId: {0}", credential.Id);
                    foreach (var eve in dbex.EntityValidationErrors)
                    {
                        message += string.Format("\rEntity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                 eve.Entry.ThisEntity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            message += string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                     ve.PropertyName, ve.ErrorMessage);
                        }

                        LoggingHelper.LogError(message, true);
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(), parentId: {0}", entity.ParentId));
                }
            }

            return(efEntity.Id);
        }