예제 #1
0
        }        //

        //public 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.Id = from.Id;
        //	to.CredentialId = from.CredentialId;
        //	to.RelationshipTypeId = from.RelationshipTypeId;
        //	to.EntityId = from.ParentId;

        //}
        public static void MapFromDB(DBEntity from, ThisEntity to, bool isForDetailPageCondition = false)
        {
            to.Id                 = from.Id;
            to.CredentialId       = from.CredentialId;
            to.ParentId           = from.EntityId;
            to.RelationshipTypeId = from.RelationshipTypeId;

            //to.Credential = from.Credential;
            to.Credential = new Credential();
            if (from.Credential != null && from.Credential.Id > 0)
            {
                to.ProfileSummary = from.Credential.Name;
                CredentialMinimumMap(from.Credential, to.Credential);
            }
            else
            {
                to.Credential = CredentialManager.GetBasic(to.CredentialId);
                if (to.Credential != null && to.Credential.Id > 0)
                {
                    to.ProfileSummary = to.Credential.Name;
                    //CredentialMinimumMap( from.Credential, to.Credential );
                }
                else
                {
                    to.ProfileSummary = string.Format("Credential ({0}) has not been downloaded", to.CredentialId);
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".MapFromDB() Credential ({0}) has not been downloaded. ParentEntityId: {1}, Entity.CredentialId: {2}", to.CredentialId, to.ParentId, to.Id));
                }
            }

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

        public 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.Id           = from.Id;
            to.CredentialId = from.CredentialId;
            to.EntityId     = from.ParentId;
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="credentialId"></param>
        /// <param name="relationshipTypeId">Values:</param>
        /// <param name="newId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private bool Add(Entity parent, int credentialId, int relationshipTypeId,
                         ref int newId, ref SaveStatus status)
        {
            bool isValid = true;
            int  count   = 0;

            newId = 0;


            if (parent == null || parent.Id == 0)
            {
                status.AddWarning(string.Format(thisClassName + ".Add() Error: a valid parent entity was not provided for credential: {0}.", credentialId));
                return(false);
            }

            if (credentialId < 1)
            {
                status.AddWarning(thisClassName + ".Add() Error: a valid credential was not provided.");
                return(false);
            }
            if (relationshipTypeId == 0)
            {
                relationshipTypeId = 1;
            }

            DBEntity efEntity = new DBEntity();

            try
            {
                using (var context = new EntityContext())
                {
                    //first check for duplicates
                    efEntity = context.Entity_Credential
                               .FirstOrDefault(s => s.EntityId == parent.Id && s.CredentialId == credentialId && s.RelationshipTypeId == relationshipTypeId);
                    if (efEntity != null && efEntity.Id > 0)
                    {
                        newId = efEntity.Id;
                        //just let it go - expected for an update
                        LoggingHelper.DoTrace(6, string.Format(thisClassName + ".Add() the credential is already part of this profile. credential: {0} to parent entityId: :{1}  ", credentialId, parent.Id));
                        return(true);
                    }

                    //add
                    efEntity = new DBEntity
                    {
                        CredentialId       = credentialId,
                        RelationshipTypeId = relationshipTypeId,
                        EntityId           = parent.Id,
                        Created            = DateTime.Now
                    };

                    context.Entity_Credential.Add(efEntity);
                    count = context.SaveChanges();

                    newId = efEntity.Id;

                    if (count == 0)
                    {
                        status.AddError(string.Format(thisClassName + ".Add() Unable to add the related credential: {0} to parent entityId: {1} ", credentialId, parent.Id));
                        isValid = false;
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
            {
                string message = HandleDBValidationError(dbex, thisClassName + ".Add()", string.Format("CredentialId: {0} to parent entityId: {1} ", credentialId, parent.Id));
                status.AddError(message);
            }
            catch (Exception ex)
            {
                string fullMessage = FormatExceptions(ex);
                string message     = thisClassName + string.Format(".Add(), CredentialId: {0} to parent entityId: {1} ", credentialId, parent.Id) + fullMessage;
                LoggingHelper.LogError(ex, message);
                status.AddError(message);
            }
            return(isValid);
        }