private IEnumerable <EdmProperty> GenerateIndependentForeignKeyColumns(
            EntityType principalEntityType,
            EntityType dependentEntityType,
            AssociationSetMapping associationSetMapping,
            EndPropertyMapping associationEndMapping,
            EntityType dependentTable,
            bool isPrimaryKeyColumn,
            NavigationProperty principalNavigationProperty)
        {
            DebugCheck.NotNull(principalEntityType);
            DebugCheck.NotNull(associationEndMapping);
            DebugCheck.NotNull(dependentTable);

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

                var foreignKeyColumn
                    = MapTableColumn(property, columnName, false);

                dependentTable.AddColumn(foreignKeyColumn);

                if (isPrimaryKeyColumn)
                {
                    dependentTable.AddKeyMember(foreignKeyColumn);
                }

                foreignKeyColumn.Nullable
                    = associationEndMapping.AssociationEnd.IsOptional() ||
                      (associationEndMapping.AssociationEnd.IsRequired() &&
                       dependentEntityType.BaseType != null);

                foreignKeyColumn.StoreGeneratedPattern = StoreGeneratedPattern.None;

                yield return(foreignKeyColumn);

                associationEndMapping.AddProperty(new ScalarPropertyMapping(property, foreignKeyColumn));

                if (foreignKeyColumn.Nullable)
                {
                    associationSetMapping
                    .AddCondition(new ConditionPropertyMapping(null, foreignKeyColumn, null, false));
                }
            }
        }
        private static EndPropertyMapping BuildEndPropertyMapping(
            AssociationSetEnd storeSetEnd,
            SimpleMappingContext mappingContext)
        {
            Debug.Assert(storeSetEnd != null, "storeSetEnd != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var endPropertyMapping =
                new EndPropertyMapping
            {
                AssociationEnd = mappingContext[storeSetEnd].CorrespondingAssociationEndMember
            };

            foreach (EdmProperty storeKeyMember in storeSetEnd.EntitySet.ElementType.KeyMembers)
            {
                var modelKeyMember     = mappingContext[storeKeyMember];
                var storeFkTableMember = GetAssociatedFkColumn(storeSetEnd, storeKeyMember);

                endPropertyMapping.AddProperty(
                    new ScalarPropertyMapping(modelKeyMember, storeFkTableMember));
            }

            return(endPropertyMapping);
        }