src/Shared/EntityFramework.SqlServerCompact/SqlDdlBuilder.cs
// <summary>
// Function for generating foreign key constraints.
// </summary>
Function for generating foreign key constraints.
Function for generating foreign key constraints .
private void AppendCreateForeignKeys(AssociationSet associationSet)
        {
            var constraintBuilder = new StringBuilder();
            Debug.Assert(associationSet.ElementType.ReferentialConstraints.Count == 1);
            var constraint = associationSet.ElementType.ReferentialConstraints[0];
            var principalEnd = associationSet.AssociationSetEnds[constraint.FromRole.Name];
            var dependentEnd = associationSet.AssociationSetEnds[constraint.ToRole.Name];
            if (ignoredEntitySets.Contains(principalEnd.EntitySet)
                || ignoredEntitySets.Contains(dependentEnd.EntitySet))
            {
                constraintBuilder.Append("-- Ignoring association set with participating entity set with defining query: ");
                AppendIdentifierEscapeNewLine(constraintBuilder, associationSet.Name);
                constraintBuilder.AppendLine();
                ignoredObjects.Add(constraintBuilder.ToString());
            }
            else
            {
                constraintBuilder.Append("ALTER TABLE ");
                AppendIdentifier(constraintBuilder, GetTableName(dependentEnd.EntitySet));
                constraintBuilder.Append(" ADD CONSTRAINT ");
                AppendIdentifier(constraintBuilder, associationSet.Name);
                constraintBuilder.Append(" FOREIGN KEY (");
                AppendIdentifiers(constraintBuilder, constraint.ToProperties); 
                constraintBuilder.Append(") REFERENCES ");
                AppendIdentifier(constraintBuilder, GetTableName(principalEnd.EntitySet));
                constraintBuilder.Append("(");
                AppendIdentifiers(constraintBuilder, constraint.FromProperties); 
                constraintBuilder.Append(")");
                if (principalEnd.CorrespondingAssociationEndMember.DeleteBehavior
                    == OperationAction.Cascade)
                {
                    constraintBuilder.Append(" ON DELETE CASCADE");
                }
                constraintBuilder.Append(";");
                constraintBuilder.AppendLine();
                constraints.Add(constraintBuilder.ToString());
            }
        }
private void AppendCreateForeignKeys ( AssociationSet associationSet ) { var constraintBuilder = new StringBuilder ( ) ; Debug . Assert ( associationSet . ElementType . ReferentialConstraints . Count == 1 ) ; var constraint = associationSet . ElementType . ReferentialConstraints [ 0 ] ; var principalEnd = associationSet . AssociationSetEnds [ constraint . FromRole . Name ] ; var dependentEnd = associationSet . AssociationSetEnds [ constraint . ToRole . Name ] ; if ( ignoredEntitySets . Contains ( principalEnd . EntitySet ) || ignoredEntitySets . Contains ( dependentEnd . EntitySet ) ) { constraintBuilder . Append ( " " ) ; AppendIdentifierEscapeNewLine ( constraintBuilder , associationSet . Name ) ; constraintBuilder . AppendLine ( ) ; ignoredObjects . Add ( constraintBuilder . ToString ( ) ) ; } else { constraintBuilder . Append ( " " ) ; AppendIdentifier ( constraintBuilder , GetTableName ( dependentEnd . EntitySet ) ) ; constraintBuilder . Append ( " " ) ; AppendIdentifier ( constraintBuilder , associationSet . Name ) ; constraintBuilder . Append ( " " ) ; AppendIdentifiers ( constraintBuilder , constraint . ToProperties ) ; constraintBuilder . Append ( " " ) ; AppendIdentifier ( constraintBuilder , GetTableName ( principalEnd . EntitySet ) ) ; constraintBuilder . Append ( " " ) ; AppendIdentifiers ( constraintBuilder , constraint . FromProperties ) ; constraintBuilder . Append ( " " ) ; if ( principalEnd . CorrespondingAssociationEndMember . DeleteBehavior == OperationAction . Cascade ) { constraintBuilder . Append ( " " ) ; } constraintBuilder . Append ( " " ) ; constraintBuilder . AppendLine ( ) ; constraints . Add ( constraintBuilder . ToString ( ) ) ; } }
Function for generating foreign key constraints.
Function for generating foreign key constraints .
// Get the constraint. // Principal (referenced) end of the constraint. // Dependent (referencing) end of constraint. //If any of the participating entity sets was skipped, skip the association too // List of referencing columns. // List of referenced columns. // Append cascade action if it exists.
associationSet
AssociationSet