protected override Expression VisitForeignKeyConstraint(SqlForeignKeyConstraintExpression foreignKeyConstraintExpression) { var action = new SqlConstraintActionExpression(SqlConstraintActionType.Add, foreignKeyConstraintExpression); var amendmentEpression = new SqlAlterTableExpression(this.currentTable.Table, action); this.amendments.Add(amendmentEpression); return(null); }
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); } }
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 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); }