static ManyToManyTableMetadata BuildTableMetadata(InternalRelationshipBuilder relationshipBuilder, Navigation tableNavigation, Type collectionType)
        {
            EntityType parentEntityType = relationshipBuilder.Metadata.PrincipalEntityType;

            // get the clr type of the intermediate entity.
            EntityType tableEntityType             = tableNavigation.GetTargetType();
            InternalEntityTypeBuilder tableBuilder = relationshipBuilder.ModelBuilder.Entity(tableEntityType.ClrType, ConfigurationSource.DataAnnotation, true);

            // metadata for the intermediate table.
            IEnumerable <Navigation> tableNavigations = tableEntityType.GetNavigations();
            Navigation end1Navigation = tableNavigations.Single(p => p.ClrType == parentEntityType.ClrType);
            Navigation end2Navigation = tableNavigations.Single(p => p.ClrType == collectionType);

            tableNavigation.Owned(ConfigurationSource.DataAnnotation);
            end1Navigation.Associated(ConfigurationSource.DataAnnotation);
            end2Navigation.Associated(ConfigurationSource.DataAnnotation);

            // if there is no FK, build one.
            if (tableEntityType.FindPrimaryKey() == null)
            {
                List <string> pkProps = tableEntityType.GetForeignKeys()
                                        .Where(fk => fk.PrincipalEntityType == parentEntityType || fk.PrincipalEntityType.ClrType == collectionType)
                                        .OrderBy(fk => fk.PrincipalEntityType != parentEntityType)
                                        .SelectMany(fk => fk.Properties)
                                        .Select(p => p.Name)
                                        .ToList();

                tableBuilder.PrimaryKey(pkProps, ConfigurationSource.Explicit);
            }

            return(new ManyToManyTableMetadata
            {
                Getter = tableNavigation.Getter,
                Setter = tableNavigation.Setter,
                End1Getter = end1Navigation.GetGetter(),
                End1Setter = end1Navigation.GetSetter(),
                End2Getter = end2Navigation.GetGetter(),
                End2Setter = end2Navigation.GetSetter(),
                ItemType = tableEntityType.ClrType
            });
        }