/// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public static bool CanHaveCheckConstraint(
            IConventionEntityType entityType,
            string name,
            string?sql,
            ConfigurationSource configurationSource)
        {
            var constraint = entityType.FindCheckConstraint(name);

            if (constraint != null)
            {
                return(constraint.Sql == sql ||
                       configurationSource.Overrides(constraint.GetConfigurationSource()));
            }

            foreach (var derivedType in entityType.GetDerivedTypes())
            {
                var derivedCheckConstraint = (IConventionCheckConstraint?)CheckConstraint.FindDeclaredCheckConstraint(derivedType, name);
                if (derivedCheckConstraint == null)
                {
                    continue;
                }

                if (derivedCheckConstraint.Sql != sql &&
                    !configurationSource.Overrides(derivedCheckConstraint.GetConfigurationSource()))
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public static IConventionCheckConstraint?HasCheckConstraint(
            IConventionEntityType entityType,
            string name,
            string?sql,
            ConfigurationSource configurationSource)
        {
            List <IConventionCheckConstraint>?checkConstraintsToBeDetached = null;
            var constraint = entityType.FindCheckConstraint(name);

            if (constraint != null)
            {
                if (constraint.Sql == sql)
                {
                    ((CheckConstraint)constraint).UpdateConfigurationSource(configurationSource);
                    return(constraint);
                }

                if (!configurationSource.Overrides(constraint.GetConfigurationSource()))
                {
                    return(null);
                }

                entityType.RemoveCheckConstraint(name);
                constraint = null;
            }
            else
            {
                foreach (var derivedType in entityType.GetDerivedTypes())
                {
                    var derivedCheckConstraint =
                        (IConventionCheckConstraint?)CheckConstraint.FindDeclaredCheckConstraint(derivedType, name);
                    if (derivedCheckConstraint == null)
                    {
                        continue;
                    }

                    if (derivedCheckConstraint.Sql != sql &&
                        !configurationSource.Overrides(derivedCheckConstraint.GetConfigurationSource()))
                    {
                        return(null);
                    }

                    if (checkConstraintsToBeDetached == null)
                    {
                        checkConstraintsToBeDetached = new List <IConventionCheckConstraint>();
                    }

                    checkConstraintsToBeDetached.Add(derivedCheckConstraint);
                }
            }

            List <IConventionCheckConstraint>?detachedCheckConstraints = null;

            if (checkConstraintsToBeDetached != null)
            {
                detachedCheckConstraints = new List <IConventionCheckConstraint>();
                foreach (var checkConstraintToBeDetached in checkConstraintsToBeDetached)
                {
                    detachedCheckConstraints.Add(
                        checkConstraintToBeDetached.EntityType.RemoveCheckConstraint(checkConstraintToBeDetached.ModelName) !);
                }
            }

            if (sql != null)
            {
                constraint = new CheckConstraint((IMutableEntityType)entityType, name, sql, configurationSource);

                if (detachedCheckConstraints != null)
                {
                    foreach (var detachedCheckConstraint in detachedCheckConstraints)
                    {
                        CheckConstraint.Attach(detachedCheckConstraint, constraint);
                    }
                }
            }

            return(constraint);
        }