private static void MapFromDB(DBEntity from, ThisEntity to)
        {
            to.Id         = from.Id;
            to.ParentId   = from.EntityId;
            to.TextTitle  = from.Title ?? "";
            to.CategoryId = from.CategoryId;
            if (to.CategoryId == CodesManager.PROPERTY_CATEGORY_PHONE_TYPE)
            {
                to.TextValue = PhoneNumber.DisplayPhone(from.TextValue);
            }
            else
            {
                to.TextValue = from.TextValue;
            }
            //else if ( to.CategoryId == CodesManager.PROPERTY_CATEGORY_ORGANIZATION_IDENTIFIERS )
            //{
            //    to.TextValue = from.TextValue;
            //}
            //else
            //{
            //    to.TextValue = from.TextValue;
            //}

            to.CodeId = from.PropertyValueId ?? 0;
            if (string.IsNullOrWhiteSpace(to.TextTitle))
            {
                to.ProfileSummary = to.TextValue;
            }
            else
            {
                to.ProfileSummary = to.TextTitle + " - " + to.TextValue;
            }
            to.CodeTitle  = "";
            to.CodeSchema = "";
            if (from.Codes_PropertyValue != null && from.Codes_PropertyValue.Id > 0)
            {
                to.CodeTitle  = from.Codes_PropertyValue.Title;
                to.CodeSchema = from.Codes_PropertyValue.SchemaName ?? "";
            }
            if (from.Entity != null && from.Entity.Id > 0)
            {
                to.EntityBaseId = from.Entity.EntityBaseId ?? 0;
                to.EntityTypeId = from.Entity.EntityTypeId;
            }
            if (IsValidDate(from.Created))
            {
                to.Created = ( DateTime )from.Created;
            }

            if (IsValidDate(from.LastUpdated))
            {
                to.LastUpdated = ( DateTime )from.LastUpdated;
            }
        }
        } //

        public bool Delete(int recordId, ref string statusMessage)
        {
            bool isOK = true;

            using (var context = new EntityContext())
            {
                DBEntity p = context.Entity_Reference.FirstOrDefault(s => s.Id == recordId);
                if (p != null && p.Id > 0)
                {
                    context.Entity_Reference.Remove(p);
                    int count = context.SaveChanges();
                }
                else
                {
                    statusMessage = string.Format("Requested record was not found: {0}", recordId);
                    isOK          = false;
                }
            }
            return(isOK);
        }
        } //

        public bool AddTextValue(string textValue,
                                 int entityId,
                                 ref SaveStatus status,
                                 int categoryId)
        {
            if (string.IsNullOrWhiteSpace(textValue))
            {
                return(true);
            }
            using (var context = new EntityContext())
            {
                //add
                DBEntity efEntity = new DBEntity();
                efEntity.CategoryId = categoryId;
                efEntity.EntityId   = entityId;
                efEntity.TextValue  = textValue;
                efEntity.Created    = efEntity.LastUpdated = DateTime.Now;

                context.Entity_Reference.Add(efEntity);
                int count = context.SaveChanges();

                if (count == 0)
                {
                    status.AddWarning(string.Format(" Unable to add Entity_Reference. EntityId: {0}, categoryId: {1}, textValue: {2}  ", entityId, categoryId, textValue));
                    return(false);
                }
                else
                {
                    if (categoryId == CodesManager.PROPERTY_CATEGORY_SUBJECT)
                    {
                        AddConnections(efEntity.Id);
                    }
                }
            }
            return(true);
        }
        } //

        /// <summary>
        /// Get single entity reference, summary
        /// </summary>
        /// <param name="entityReferenceId"></param>
        /// <returns></returns>
        //public static ThisEntity Get( int entityReferenceId )
        //{
        //    ThisEntity entity = new ThisEntity();
        //    if ( entityReferenceId == 0 )
        //    {
        //        return entity;
        //    }
        //    try
        //    {
        //        using ( var context = new ViewContext() )
        //        {
        //            Entity_Reference_Summary item = context.Entity_Reference_Summary
        //                    .SingleOrDefault( s => s.EntityReferenceId == entityReferenceId );

        //            if ( item != null && item.EntityReferenceId > 0 )
        //            {
        //                MapFromDB( item, entity );
        //            }
        //        }
        //    }
        //    catch ( Exception ex )
        //    {
        //        LoggingHelper.LogError( ex, thisClassName + ".Entity_Get" );
        //    }
        //    return entity;
        //}//
        private static void MapToDB(ThisEntity from, DBEntity to)
        {
            //want to ensure fields from create are not wiped
            if (to.Id == 0)
            {
                //if ( IsValidDate( from.Created ) )
                //	to.Created = from.Created;
                //to.CreatedById = from.CreatedById;
            }
            //to.Id = from.Id;
            to.CategoryId = from.CategoryId;

            //in some cases may not require text, so fill with empty string
            to.Title = !string.IsNullOrWhiteSpace(from.TextTitle) ? from.TextTitle : "";
            if (to.CategoryId == CodesManager.PROPERTY_CATEGORY_PHONE_TYPE)
            {
                to.TextValue = PhoneNumber.StripPhone(GetData(from.TextValue));
            }
            else if (to.CategoryId == CodesManager.PROPERTY_CATEGORY_ORGANIZATION_IDENTIFIERS)
            {
                //if other, notify admin, and need to handle separately
                if (from.CodeId == 88)
                {
                    if (from.Id == 0)
                    {
                        //will want to create a property value, and send email
                        //could append to text for now
                        //op.OtherValue += "{" + ( frameworkName ?? "missing framework name" ) + "; " + schemaUrl + "}";
                        LoggingHelper.DoTrace(2, "A new organization identifier of 'other' has been added:" + from.TextValue);
                        SendNewOtherIdentityNotice(from);
                    }
                }
                else
                {
                    //should ignore to.Title
                    to.Title = "";
                }
                to.TextValue = from.TextValue;
            }
            else
            {
                to.TextValue = from.TextValue;
            }

            if (from.CodeId > 0)
            {
                to.PropertyValueId = from.CodeId;
            }
            else if (!string.IsNullOrWhiteSpace(from.CodeSchema))
            {
                CodeItem item = CodesManager.GetPropertyBySchema(to.CategoryId, from.CodeSchema);
                if (item != null && item.Id > 0)
                {
                    to.PropertyValueId = item.Id;
                    if (string.IsNullOrWhiteSpace(to.Title))
                    {
                        to.Title = item.Title;
                    }
                }
            }
            else
            {
                to.PropertyValueId = null;
            }
        }
        } //

        //public bool AddLanguage( string textValue,
        //	   int entityId,
        //	   ref SaveStatus status,
        //	   int categoryId )
        //{
        //	if ( string.IsNullOrWhiteSpace( textValue ) )
        //	{
        //		return true;
        //	}
        //	using ( var context = new EntityContext() )
        //	{
        //		EnumeratedItem code = new EnumeratedItem();
        //		code = CodesManager.GetLanguage( textValue );
        //		if ( code.Id > 0 )
        //		{
        //			textValue = string.Format( "{0} ({1})", code.Name, code.Value );
        //			AddTextValue( textValue, entityId, ref status, categoryId );
        //		}

        //		else
        //		{
        //			status.AddWarning( thisClassName + string.Format( ". Warning - the langugage code was not found. parentUid: {0}, languagecode: {1}", entityId, textValue ) );
        //		}
        //	}
        //	return true;
        //}
        /// <summary>
        /// Persist Entity Reference
        /// </summary>
        /// <param name="profiles"></param>
        /// <param name="parentUid"></param>
        /// <param name="parentTypeId"></param>
        /// <param name="status"></param>
        /// <param name="categoryId"></param>
        /// <param name="isTitleRequired">If true, a title must exist</param>
        /// <returns></returns>
        public bool Add(List <ThisEntity> profiles,
                        Guid parentUid,
                        int parentTypeId,
                        ref SaveStatus status,
                        int categoryId,
                        bool isTitleRequired)
        {
            if (profiles == null || profiles.Count == 0)
            {
                return(true);
            }

            bool isValid = true;

            status.HasSectionErrors = false;

            if (!IsValidGuid(parentUid))
            {
                status.AddError("Error: the parent identifier was not provided.");
            }
            if (parentTypeId == 0)
            {
                status.AddError("Error: the parent type was not provided.");
            }
            if (status.HasSectionErrors)
            {
                return(false);
            }

            int count = 0;

            if (profiles == null)
            {
                profiles = new List <ThisEntity>();
            }

            DBEntity efEntity = new DBEntity();

            //Views.Entity_Summary parent2 = EntityManager.GetDBEntity( parentUid );
            Entity parent = EntityManager.GetEntity(parentUid);

            if (parent == null || parent.Id == 0)
            {
                status.AddError(thisClassName + string.Format(". Error - the parent entity was not found. parentUid: {0}", parentUid));
                return(false);
            }
            using (var context = new EntityContext())
            {
                //check add/updates first
                if (profiles.Count() > 0)
                {
                    bool isEmpty = false;

                    foreach (ThisEntity entity in profiles)
                    {
                        entity.CategoryId = categoryId;
                        if (Validate(entity, isTitleRequired, ref isEmpty, ref status) == false)
                        {
                            isValid = false;
                            continue;
                        }

                        if (isEmpty)   //skip
                        {
                            continue;
                        }
                        entity.EntityBaseId = parent.EntityBaseId;

                        //21-03-31 existance check
                        //var exists = context.Entity_Reference
                        //	.Where( s => s.EntityId == parent.Id && s.CategoryId == categoryId && s.TextValue == entity.TextValue )
                        //	.ToList();
                        //if (exists != null && exists.Any())
                        //{
                        //	return true;
                        //}
                        //if ( entity.Id == 0 )
                        //{
                        //add
                        efEntity = new DBEntity();
                        MapToDB(entity, efEntity);
                        efEntity.EntityId = parent.Id;
                        efEntity.Created  = efEntity.LastUpdated = DateTime.Now;

                        context.Entity_Reference.Add(efEntity);
                        count = context.SaveChanges();
                        //update profile record so doesn't get deleted
                        entity.Id       = efEntity.Id;
                        entity.ParentId = parent.Id;

                        if (count == 0)
                        {
                            status.AddWarning(string.Format(" Unable to add Profile: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.TextTitle) ? "no description" : entity.TextTitle));
                        }
                        else
                        {
                            if (categoryId == CodesManager.PROPERTY_CATEGORY_SUBJECT)
                            {
                                AddConnections(entity.Id);
                            }
                        }
                    } //foreach
                }
            }

            return(isValid);
        } //
        } //

        /// <summary>
        /// Add for simple strings - textValue only
        /// </summary>
        /// <param name="profiles"></param>
        /// <param name="parentUid"></param>
        /// <param name="status"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public bool AddTextValue(List <string> profiles,
                                 Guid parentUid,
                                 ref SaveStatus status,
                                 int categoryId)
        {
            bool isValid = true;

            if (profiles == null || profiles.Count == 0)
            {
                return(true);
            }
            status.HasSectionErrors = false;

            if (!IsValidGuid(parentUid))
            {
                status.AddError("Error: the parent identifier was not provided.");
                return(false);
            }

            //int count = 0;
            DBEntity efEntity = new DBEntity();
            Entity   parent   = EntityManager.GetEntity(parentUid);

            if (parent == null || parent.Id == 0)
            {
                status.AddError(thisClassName + string.Format(". Error - the parent entity was not found. parentUid: {0}", parentUid));
                return(false);
            }
            //using ( var context = new EntityContext() )
            //{
            foreach (string textValue in profiles)
            {
                if (string.IsNullOrWhiteSpace(textValue))
                {
                    continue;
                }
                AddTextValue(textValue, parent.Id, ref status, categoryId);
                ////add
                //efEntity = new DBEntity();
                //efEntity.CategoryId = categoryId;
                //efEntity.EntityId = parent.Id;
                //efEntity.TextValue = textValue;
                //efEntity.Created = efEntity.LastUpdated = DateTime.Now;

                //context.Entity_Reference.Add( efEntity );
                //count = context.SaveChanges();

                //if ( count == 0 )
                //{
                //	status.AddWarning( string.Format( " Unable to add Entity_Reference. EntityId: {0}, categoryId: {1}, textValue: {2}  ", parent.Id, categoryId, textValue ));
                //}
                //else
                //{
                //	if ( categoryId == CodesManager.PROPERTY_CATEGORY_SUBJECT )
                //		AddConnections( efEntity.Id );
                //}
            } //foreach
              //}

            return(isValid);
        } //