Example #1
0
        protected virtual void ProcessRange(
            IConventionProperty property,
            MemberInfo memberInfo,
            string tableName,
            string columnName,
            StringBuilder sql)
        {
            if (!(memberInfo.GetCustomAttribute <RangeAttribute>() is RangeAttribute attribute) ||
                !(property.FindTypeMapping() is RelationalTypeMapping typeMapping) ||
                attribute.Minimum.GetType() != typeMapping.ClrType ||
                attribute.Maximum.GetType() != typeMapping.ClrType)
            {
                return;
            }

            sql.Clear();

            sql
            .Append(_sqlGenerationHelper.DelimitIdentifier(columnName))
            .Append(" >= ")
            .Append(typeMapping.GenerateSqlLiteral(attribute.Minimum))
            .Append(" AND ")
            .Append(_sqlGenerationHelper.DelimitIdentifier(columnName))
            .Append(" <= ")
            .Append(typeMapping.GenerateSqlLiteral(attribute.Maximum));

            var constraintName = $"CK_{tableName}_{columnName}_Range";

            property.DeclaringEntityType.AddCheckConstraint(constraintName, sql.ToString());
        }