private static void MoveAssociationSetMappingDependents(
            DbAssociationSetMapping associationSetMapping,
            DbAssociationEndMapping dependentMapping,
            DbTableMetadata toTable,
            bool useExistingColumns)
        {
            Contract.Requires(associationSetMapping != null);
            Contract.Requires(dependentMapping != null);
            Contract.Requires(toTable != null);

            dependentMapping.PropertyMappings.Each(
                pm =>
                    {
                        var oldColumn = pm.Column;
                        pm.Column = TableOperations.MoveColumnAndAnyConstraints(
                            associationSetMapping.Table, toTable, oldColumn, useExistingColumns);
                        associationSetMapping.ColumnConditions.Where(cc => cc.Column == oldColumn).Each(
                            cc =>
                            cc.Column = pm.Column);
                    });

            associationSetMapping.Table = toTable;
        }
        private void GenerateIndependentForeignKeyColumns(
            EdmEntityType principalEntityType,
            EdmEntityType dependentEntityType,
            DbAssociationSetMapping associationSetMapping,
            DbAssociationEndMapping associationEndMapping,
            DbTableMetadata dependentTable,
            DbForeignKeyConstraintMetadata foreignKeyConstraint,
            bool isPrimaryKeyColumn,
            EdmNavigationProperty principalNavigationProperty)
        {
            //Contract.Requires(principalEntityType != null);
            //Contract.Requires(associationEndMapping != null);
            //Contract.Requires(dependentTable != null);
            //Contract.Requires(foreignKeyConstraint != null);

            foreach (var property in principalEntityType.KeyProperties())
            {
                var foreignKeyColumn
                    = dependentTable.AddColumn(
                        ((principalNavigationProperty != null)
                             ? principalNavigationProperty.Name
                             : principalEntityType.Name)
                        + "_" + property.Name);

                MapTableColumn(property, foreignKeyColumn, false, isPrimaryKeyColumn);

                foreignKeyColumn.IsNullable = (associationEndMapping.AssociationEnd.IsOptional()
                                               ||
                                               (associationEndMapping.AssociationEnd.IsRequired()
                                                && dependentEntityType.BaseType != null));
                foreignKeyColumn.StoreGeneratedPattern = DbStoreGeneratedPattern.None;

                foreignKeyConstraint.DependentColumns.Add(foreignKeyColumn);

                associationEndMapping.PropertyMappings.Add(
                    new DbEdmPropertyMapping
                        {
                            Column = foreignKeyColumn,
                            PropertyPath = new[] { property }
                        });

                if (foreignKeyColumn.IsNullable)
                {
                    associationSetMapping.ColumnConditions.Add(
                        new DbColumnCondition
                            {
                                Column = foreignKeyColumn,
                                IsNull = false
                            });
                }
            }
        }
 private void WriteAssociationEndMappingElement(DbAssociationEndMapping endMapping)
 {
     _xmlWriter.WriteStartElement(MslConstants.Element_EndProperty);
     _xmlWriter.WriteAttributeString(MslConstants.Attribute_Name, endMapping.AssociationEnd.Name);
     foreach (var propertyMapping in endMapping.PropertyMappings)
     {
         WriteScalarPropertyElement(propertyMapping.PropertyPath.First(), propertyMapping.Column);
     }
     _xmlWriter.WriteEndElement();
 }
        private void GenerateIndependentForeignKeyConstraint(
            DbDatabaseMapping databaseMapping,
            EdmEntityType principalEntityType,
            EdmEntityType dependentEntityType,
            DbTableMetadata dependentTable,
            DbAssociationSetMapping associationSetMapping,
            DbAssociationEndMapping associationEndMapping,
            string name,
            EdmAssociationEnd principalEnd,
            bool isPrimaryKeyColumn = false)
        {
            //Contract.Requires(databaseMapping != null);
            //Contract.Requires(principalEntityType != null);
            //Contract.Requires(dependentTable != null);
            //Contract.Requires(associationEndMapping != null);
            //Contract.Requires(!string.IsNullOrWhiteSpace(name));

            var principalTable
                = GetEntityTypeMappingInHierarchy(databaseMapping, principalEntityType)
                    .TypeMappingFragments
                    .Single()
                    .Table;

            var foreignKeyConstraint = new DbForeignKeyConstraintMetadata
                {
                    Name = name,
                    PrincipalTable = principalTable,
                    DeleteAction = associationEndMapping.AssociationEnd.DeleteAction.HasValue
                                       ? (DbOperationAction)
                                         associationEndMapping.AssociationEnd.DeleteAction.
                                             Value
                                       : DbOperationAction.None
                };

            var principalNavigationProperty
                = databaseMapping.Model.GetEntityTypes()
                    .SelectMany(e => e.DeclaredNavigationProperties)
                    .SingleOrDefault(n => n.ResultEnd == principalEnd);

            GenerateIndependentForeignKeyColumns(
                principalEntityType,
                dependentEntityType,
                associationSetMapping,
                associationEndMapping,
                dependentTable,
                foreignKeyConstraint,
                isPrimaryKeyColumn,
                principalNavigationProperty);

            dependentTable.ForeignKeyConstraints.Add(foreignKeyConstraint);
        }