Rock.Model.EntityTypeService.Get C# (CSharp) Method

Get() public method

Gets the specified Rock.Model.EntityType by the object type. If a match is not found, it can optionally create a new Rock.Model.EntityType for the object.
public Get ( Type type, bool createIfNotFound, PersonAlias personAlias ) : EntityType
type System.Type The to search for.
createIfNotFound bool A value that indicates if a new should be created if a match is not found. This value /// will be true if a new should be created if there is not a match; otherwise false/
personAlias PersonAlias A representing the alias of the who is searching for and possibly creating a new EntityType. This value can be /// null if the logged in person is not known (i.e. an anonymous user).
return EntityType
        public EntityType Get( Type type, bool createIfNotFound, PersonAlias personAlias )
        {
            var entityType = Get( type.FullName );
            if ( entityType != null )
            {
                return entityType;
            }

            if ( createIfNotFound )
            {
                // Create a new context so type can be saved independing of current context
                using ( var rockContext = new RockContext() )
                {
                    var EntityTypeService = new EntityTypeService( rockContext );
                    entityType = new EntityType();
                    entityType.Name = type.FullName;
                    entityType.FriendlyName = type.Name.SplitCase();
                    entityType.AssemblyName = type.AssemblyQualifiedName;
                    EntityTypeService.Add( entityType );
                    rockContext.SaveChanges();
                }

                // Read type using current context
                return this.Get( entityType.Id );
            }

            return null;
        }

Same methods

EntityTypeService::Get ( string entityName ) : EntityType
EntityTypeService::Get ( string name, bool createIfNotFound ) : EntityType

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Handles the SaveClick event of the mdEdit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void mdEdit_SaveClick( object sender, EventArgs e )
        {
            var rockContext = new RockContext();
            EntityTypeService entityTypeService = new EntityTypeService( rockContext );
            EntityType entityType = entityTypeService.Get( int.Parse( hfEntityTypeId.Value ) );

            if ( entityType == null )
            {
                entityType = new EntityType();
                entityType.IsEntity = true;
                entityType.IsSecured = true;
                entityTypeService.Add( entityType );
            }

            entityType.Name = tbName.Text;
            entityType.FriendlyName = tbFriendlyName.Text;
            entityType.IsCommon = cbCommon.Checked;

            rockContext.SaveChanges();

            EntityTypeCache.Flush( entityType.Id );

            hfEntityTypeId.Value = string.Empty;

            HideDialog();

            BindGrid();
        }
All Usage Examples Of Rock.Model.EntityTypeService::Get