public HasIndexFluent HasIndex(Expression <Func <T, object> > index)
        {
            var exp = index?.Body;

            if (exp?.NodeType == ExpressionType.Convert)
            {
                exp = (exp as UnaryExpression)?.Operand;
            }
            if (exp == null)
            {
                throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("index"));
            }

            var indexName = $"idx_{Guid.NewGuid().ToString("N").Substring(0, 8)}";
            var columns   = new List <string>();

            switch (exp.NodeType)
            {
            case ExpressionType.MemberAccess:
                columns.Add((exp as MemberExpression).Member.Name);
                break;

            case ExpressionType.New:
                foreach (var member in (exp as NewExpression).Members)
                {
                    columns.Add(member.Name);
                }
                break;
            }
            _tf.Index(indexName, string.Join(", ", columns), false);
            return(new HasIndexFluent(_tf, indexName, columns));
        }
        public HasIndexFluent HasIndex(Expression <Func <T, object> > index)
        {
            if (index?.Body == null)
            {
                throw new ArgumentException("参数错误 index 不能为 null");
            }
            var exp = index.Body;

            var indexName = $"idx_{Guid.NewGuid().ToString("N").Substring(0, 8)}";
            var columns   = new List <string>();

            switch (exp.NodeType)
            {
            case ExpressionType.MemberAccess:
                columns.Add((exp as MemberExpression).Member.Name);
                break;

            case ExpressionType.New:
                foreach (var member in (exp as NewExpression).Members)
                {
                    columns.Add(member.Name);
                }
                break;
            }
            _tf.Index(indexName, string.Join(", ", columns), false);
            return(new HasIndexFluent(_tf, indexName, columns));
        }
Beispiel #3
0
        public HasIndexFluent HasIndex(string index)
        {
            if (index == null)
            {
                throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("index"));
            }
            var indexName = $"idx_{Guid.NewGuid().ToString("N").Substring(0, 8)}";
            var columns   = new List <string>();

            foreach (string name in index.Split(','))
            {
                if (string.IsNullOrEmpty(name.Trim()))
                {
                    continue;
                }
                columns.Add(name.Trim());
            }
            _tf.Index(indexName, string.Join(", ", columns), false);
            return(new HasIndexFluent(_tf, indexName, columns));
        }
 public HasIndexFluent IsUnique()
 {
     _isUnique = true;
     _modelBuilder.Index(_indexName, string.Join(", ", _columns), _isUnique);
     return(this);
 }