protected override Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression foreignKeyConstraintExpression)
        {
            var action = new SqlConstraintActionExpression(SqlConstraintActionType.Add, foreignKeyConstraintExpression);
            var ammendmentEpression = new SqlAlterTableExpression(currentTable.Table, action);

            ammendments.Add(ammendmentEpression);

            return null;
        }
Esempio n. 2
0
        protected virtual Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression foreignKeyConstraintExpression)
        {
            var referencesColumnExpression = (SqlReferencesColumnExpression)this.Visit(foreignKeyConstraintExpression.ReferencesColumnExpression);

            if (referencesColumnExpression != foreignKeyConstraintExpression.ReferencesColumnExpression)
            {
                return(new SqlForeignKeyConstraintExpression(foreignKeyConstraintExpression.ConstraintName, foreignKeyConstraintExpression.ColumnNames, referencesColumnExpression));
            }
            else
            {
                return(foreignKeyConstraintExpression);
            }
        }
        protected override Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression foreignKeyConstraintExpression)
        {
            string primaryKeyName;

            if (this.primaryKeyNameByTablesWithReducedPrimaryKeyName.TryGetValue(foreignKeyConstraintExpression.ReferencesColumnExpression.ReferencedTable.Name, out primaryKeyName))
            {
                var index = foreignKeyConstraintExpression.ReferencesColumnExpression.ReferencedColumnNames.IndexOf(primaryKeyName);

                var newColumnNames = foreignKeyConstraintExpression.ColumnNames.Where((c, i) => i == index);
                var newReferencedColumnNames = foreignKeyConstraintExpression.ReferencesColumnExpression.ReferencedColumnNames.Where((c, i) => i == index);

                return foreignKeyConstraintExpression.UpdateColumnNamesAndReferencedColumnExpression(newColumnNames, foreignKeyConstraintExpression.ReferencesColumnExpression.UpdateReferencedColumnNames(newReferencedColumnNames));
            }

            return base.VisitForeignKeyConstraint(foreignKeyConstraintExpression);
        }
 protected override Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression expression)
 {
     this.hashCode ^= expression.ConstraintName?.GetHashCode() ?? 0;
     return base.VisitForeignKeyConstraint(expression);
 }
        protected override Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression expression)
        {
            SqlForeignKeyConstraintExpression current;
            if (!TryGetCurrent(expression, out current))
            {
                return expression;
            }

            if (!(this.result &= object.Equals(current.ConstraintName, expression.ConstraintName)))
            {
                return expression;
            }

            if (!(this.result &= current.NodeType == expression.NodeType))
            {
                return expression;
            }

            if (!(this.result &= current.Type == expression.Type))
            {
                return expression;
            }

            this.currentObject = current.ReferencesColumnExpression;
            this.VisitReferencesColumn(expression.ReferencesColumnExpression);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current.ColumnNames;
            this.VisitObjectList(expression.ColumnNames);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current;
            return expression;
        }
Esempio n. 6
0
        protected override Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression foreignKeyConstraintExpression)
        {
            if (foreignKeyConstraintExpression.ConstraintName != null)
            {
                this.Write("CONSTRAINT ");
                this.WriteQuotedIdentifier(foreignKeyConstraintExpression.ConstraintName);
                this.Write(" ");
            }

            this.Write("FOREIGN KEY(");
            this.WriteDeliminatedListOfItems(foreignKeyConstraintExpression.ColumnNames, this.WriteQuotedIdentifier);
            this.Write(") ");

            this.Visit(foreignKeyConstraintExpression.ReferencesColumnExpression);

            return foreignKeyConstraintExpression;
        }
Esempio n. 7
0
		protected virtual Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression foreignKeyConstraintExpression)
		{
			var referencesColumnExpression = (SqlReferencesColumnExpression)this.Visit(foreignKeyConstraintExpression.ReferencesColumnExpression);

			if (referencesColumnExpression != foreignKeyConstraintExpression.ReferencesColumnExpression)
			{
				return new SqlForeignKeyConstraintExpression(foreignKeyConstraintExpression.ConstraintName, foreignKeyConstraintExpression.ColumnNames, referencesColumnExpression);
			}
			else
			{
				return foreignKeyConstraintExpression;
			}
		}
 protected override Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression expression)
 {
     this.hashCode ^= expression.ConstraintName?.GetHashCode() ?? 0;
     return(base.VisitForeignKeyConstraint(expression));
 }
        private IEnumerable<SqlColumnDefinitionExpression> BuildForeignKeyColumnDefinitions(PropertyDescriptor referencingProperty, ColumnInfo[] columnInfos)
        {
            var relatedPropertyTypeDescriptor = this.model.GetTypeDescriptor(referencingProperty.PropertyType);
            var referencedTableName = relatedPropertyTypeDescriptor.PersistedName;

            var valueRequired = (referencingProperty.ValueRequiredAttribute != null && referencingProperty.ValueRequiredAttribute.Required)
                || referencingProperty.IsPrimaryKey;
            var supportsInlineForeignKeys = this.sqlDialect.SupportsFeature(SqlFeature.InlineForeignKeys);

            var foreignObjectConstraintAttribute = referencingProperty.ForeignObjectConstraintAttribute;

            foreach (var foreignKeyColumn in columnInfos)
            {
                var retval = this.BuildColumnDefinition(foreignKeyColumn);

                if (columnInfos.Length == 1 && supportsInlineForeignKeys)
                {
                    var names = new[] { foreignKeyColumn.DefinitionProperty.PersistedName };
                    var newConstraints = new List<Expression>(retval.ConstraintExpressions);

                    var referencesColumnExpression = new SqlReferencesColumnExpression
                    (
                        new SqlTableExpression(referencedTableName),
                        SqlColumnReferenceDeferrability.InitiallyDeferred,
                        names,
                        FixAction((foreignObjectConstraintAttribute != null && ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnDeleteAction) != null) ? ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnDeleteAction).Value : (valueRequired ? SqlColumnReferenceAction.Restrict : SqlColumnReferenceAction.SetNull)),
                        FixAction((foreignObjectConstraintAttribute != null && ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnDeleteAction) != null) ? ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnUpdateAction).Value : SqlColumnReferenceAction.NoAction)
                    );

                    newConstraints.Add(referencesColumnExpression);

                    retval = new SqlColumnDefinitionExpression(retval.ColumnName, retval.ColumnType, newConstraints);
                }

                yield return retval;
            }

            if (columnInfos.Length > 1 || !supportsInlineForeignKeys)
            {
                var currentTableColumnNames = columnInfos.Select(c => c.ColumnName);
                var referencedTableColumnNames = columnInfos.Select(c => c.GetTailColumnName());

                var referencesColumnExpression = new SqlReferencesColumnExpression
                (
                    new SqlTableExpression(referencedTableName),
                    SqlColumnReferenceDeferrability.InitiallyDeferred,
                    referencedTableColumnNames,
                    FixAction((foreignObjectConstraintAttribute != null && ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnDeleteAction) != null) ? ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnDeleteAction).Value : (valueRequired ? SqlColumnReferenceAction.Restrict : SqlColumnReferenceAction.SetNull)),
                    FixAction((foreignObjectConstraintAttribute != null && ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnDeleteAction) != null) ? ToSqlColumnReferenceAction(foreignObjectConstraintAttribute.OnUpdateAction).Value : SqlColumnReferenceAction.NoAction)
                );

                var foreignKeyConstraint = new SqlForeignKeyConstraintExpression(null, currentTableColumnNames, referencesColumnExpression);

                currentTableConstraints.Add(foreignKeyConstraint);
            }
        }