Exemple #1
0
        /// <summary>
        /// 添加外键。
        /// </summary>
        /// <typeparam name="TPrincipal">主键类。</typeparam>
        /// <param name="columns">字段。</param>
        /// <param name="principalColumns">主键列。</param>
        /// <param name="onUpdate">更新时候对应的操作。</param>
        /// <param name="onDelete">删除时候对应的操作。</param>
        /// <param name="action">添加扩展操作。</param>
        /// <returns>返回迁移构建实例。</returns>
        public virtual CreateTableBuilder <TEntity> ForeignKey <TPrincipal>(
            [NotNull] Expression <Func <TEntity, object> > columns,
            [NotNull] Expression <Func <TPrincipal, object> > principalColumns = null,
            ReferentialAction onUpdate = ReferentialAction.NoAction,
            ReferentialAction onDelete = ReferentialAction.NoAction,
            Action <OperationBuilder <AddForeignKeyOperation> > action = null)
        {
            Check.NotNull(columns, nameof(columns));

            var operation = new AddForeignKeyOperation
            {
                Table          = Operation.Table,
                Columns        = columns.GetPropertyNames(),
                PrincipalTable = _model.GetTable(typeof(TPrincipal)),
                OnUpdate       = onUpdate,
                OnDelete       = onDelete
            };

            if (principalColumns == null)
            {
                operation.PrincipalColumns = operation.Columns;
            }
            else
            {
                operation.PrincipalColumns = principalColumns.GetPropertyNames();
            }
            operation.Name = OperationHelper.GetName(NameType.ForeignKey, operation.Table, operation.Columns, operation.PrincipalTable);
            Operation.ForeignKeys.Add(operation);

            action?.Invoke(new OperationBuilder <AddForeignKeyOperation>(operation));
            return(this);
        }
Exemple #2
0
        /// <summary>
        /// 添加唯一键。
        /// </summary>
        /// <param name="columns">列。</param>
        /// <param name="action">添加扩展信息。</param>
        /// <returns>返回迁移构建实例。</returns>
        public virtual CreateTableBuilder <TEntity> UniqueConstraint(
            [NotNull] Expression <Func <TEntity, object> > columns,
            Action <OperationBuilder <AddUniqueConstraintOperation> > action = null)
        {
            Check.NotNull(columns, nameof(columns));

            var operation = new AddUniqueConstraintOperation
            {
                Table   = Operation.Table,
                Columns = columns.GetPropertyNames()
            };

            operation.Name = OperationHelper.GetName(NameType.UniqueKey, operation.Table, operation.Columns);
            Operation.UniqueConstraints.Add(operation);

            action?.Invoke(new OperationBuilder <AddUniqueConstraintOperation>(operation));
            return(this);
        }
Exemple #3
0
        /// <summary>
        /// 添加主键。
        /// </summary>
        /// <param name="action">添加扩展信息。</param>
        /// <returns>返回迁移构建实例。</returns>
        public virtual CreateTableBuilder <TEntity> PrimaryKey(Action <OperationBuilder <AddPrimaryKeyOperation> > action = null)
        {
            var key = _model.GetEntity(typeof(TEntity)).PrimaryKey;

            if (key == null)
            {
                return(this);
            }

            var operation = new AddPrimaryKeyOperation
            {
                Table   = Operation.Table,
                Columns = key.Properties.Select(p => p.Name).ToArray()
            };

            operation.Name       = OperationHelper.GetName(NameType.PrimaryKey, operation.Table);
            Operation.PrimaryKey = operation;

            action?.Invoke(new OperationBuilder <AddPrimaryKeyOperation>(operation));
            return(this);
        }