Exemple #1
0
        /// <summary>
        /// Attaches a Bean to this bean in a manner of a 1:n reference. The Bean to attach
        /// has the Foreign key that references to this Bean's Id.
        /// If the Foreign Key Name of the (owned) Bean differs form the
        /// Name of the Owner Bean, it is possible to pass an alias name.
        /// </summary>
        /// <param name="bean">Bean to be attached. It references the current Bean via Foreign Key.</param>
        /// <param name="fkAlias">Alias for the Owner Bean's Foreign Key Name (w/o Primary Key Name)</param>
        /// <returns>true, if successful</returns>
        public bool AttachOwned(Bean bean, string fkAlias = "")
        {
            var foreignKey   = GetFkName(fkAlias == string.Empty ? GetKind() : fkAlias);
            var relatingKind = bean.GetKind();

            if (!Api.IsKnownKindColumn(relatingKind, foreignKey))
            {
                throw MissingForeignKeyColumnException.Create(relatingKind, foreignKey);
            }

            bean
            .Put(foreignKey, GetKeyValue())
            .Store();

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Detaches a referencing Bean from the current Bean. The referencing bean
        /// may be trashed (deleted) or retained in the table but then as orphaned Bean.
        /// If the Foreign Key Name of the (owned) Bean differs form the
        /// Name of the Owner Bean, it is possible to pass an alias name.
        /// </summary>
        /// <param name="bean">Bean to be detached.</param>
        /// <param name="trashOwned">Delete or retain Bean as orphaned Bean.</param>
        /// <param name="fkAlias">Alias for the Owner Bean's Foreign Key Name (w/o Primary Key Name)</param>
        /// <returns>true, if successful</returns>
        public bool DetachOwned(Bean bean, bool trashOwned = false, string fkAlias = "")
        {
            if (trashOwned)
            {
                bean.Trash();
            }
            else
            {
                var foreignKey = GetFkName(fkAlias == string.Empty ? GetKind() : fkAlias);

                if (!Api.IsKnownKindColumn(bean.GetKind(), foreignKey))
                {
                    throw MissingForeignKeyColumnException.Create(bean.GetKind(), foreignKey);
                }

                bean
                .Put(foreignKey, null)
                .Store();
            }

            return(true);
        }