public EdmModel Build(StoreSchemaDetails storeSchemaDetails)
        {
            Debug.Assert(storeSchemaDetails != null, "storeSchemaDetails != null");

            var entityRegister = new EntityRegister();
            CreateEntitySets(
                storeSchemaDetails.TableDetails,
                storeSchemaDetails.ViewDetails,
                entityRegister);

            var associationTypes = new List<AssociationType>();
            var associationSets =
                CreateAssociationSets(
                    storeSchemaDetails.RelationshipDetails,
                    entityRegister,
                    associationTypes);

            var entityContainer =
                EntityContainer.Create(
                    _namespaceName.Replace(".", string.Empty) + "Container",
                    DataSpace.SSpace,
                    entityRegister.EntitySets.Union<EntitySetBase>(associationSets),
                    null,
                    null);

            var storeModel =
                EdmModel.CreateStoreModel(
                    entityContainer,
                    new DbProviderInfo(_providerInvariantName, _providerManifestToken),
                    null,
                    EntityFrameworkVersion.VersionToDouble(_targetEntityFrameworkVersion));

            foreach (var entityType in entityRegister.EntityTypes)
            {
                storeModel.AddItem(entityType);
            }

            foreach (var associationType in associationTypes)
            {
                storeModel.AddItem(associationType);
            }

            var functions = CreateFunctions(storeSchemaDetails.FunctionDetails, storeSchemaDetails.TVFReturnTypeDetails);
            foreach (var function in functions)
            {
                storeModel.AddItem(function);
            }

            return storeModel;
        }
        public void StoreSchemaDetails_initialized_correctly()
        {
            var tableDetails         = Enumerable.Empty <TableDetailsRow>();
            var viewDetails          = Enumerable.Empty <TableDetailsRow>();
            var relationshipDetails  = Enumerable.Empty <RelationshipDetailsRow>();
            var functionDetails      = Enumerable.Empty <FunctionDetailsRowView>();
            var tvfReturnTypeDetails = Enumerable.Empty <TableDetailsRow>();

            var storeSchemaDetails = new StoreSchemaDetails(
                tableDetails, viewDetails, relationshipDetails, functionDetails, tvfReturnTypeDetails);

            Assert.Same(tableDetails, storeSchemaDetails.TableDetails);
            Assert.Same(viewDetails, storeSchemaDetails.ViewDetails);
            Assert.Same(relationshipDetails, storeSchemaDetails.RelationshipDetails);
            Assert.Same(functionDetails, storeSchemaDetails.FunctionDetails);
            Assert.Same(tvfReturnTypeDetails, storeSchemaDetails.TVFReturnTypeDetails);
        }
        public void StoreSchemaDetails_initialized_correctly()
        {
            var tableDetails = Enumerable.Empty<TableDetailsRow>();
            var viewDetails = Enumerable.Empty<TableDetailsRow>();
            var relationshipDetails = Enumerable.Empty<RelationshipDetailsRow>();
            var functionDetails = Enumerable.Empty<FunctionDetailsRowView>();
            var tvfReturnTypeDetails = Enumerable.Empty<TableDetailsRow>();

            var storeSchemaDetails = new StoreSchemaDetails(
                tableDetails, viewDetails, relationshipDetails, functionDetails, tvfReturnTypeDetails);

            Assert.Same(tableDetails, storeSchemaDetails.TableDetails);
            Assert.Same(viewDetails, storeSchemaDetails.ViewDetails);
            Assert.Same(relationshipDetails, storeSchemaDetails.RelationshipDetails);
            Assert.Same(functionDetails, storeSchemaDetails.FunctionDetails);
            Assert.Same(tvfReturnTypeDetails, storeSchemaDetails.TVFReturnTypeDetails);
        }
            public static void GenerateAssociationType_from_store_association_type_creates_non_FK_association_if_EF_version1()
            {
                var tableDetails = new[]
                    {
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "A", "Id", 0, false, "int", isIdentiy: true, isPrimaryKey: true),
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "B", "Id", 0, false, "int", isIdentiy: true, isPrimaryKey: true)
                    };

                var relationshipDetails = new[]
                    {
                        StoreModelBuilderTests.CreateRelationshipDetailsRow(
                            "R1", "R1", 0, false, "catalog", "schema", "A", "Id", "catalog", "schema", "B", "Id")
                    };

                var storeSchemaDetails = new StoreSchemaDetails(
                    tableDetails,
                    new TableDetailsRow[0],
                    relationshipDetails,
                    new FunctionDetailsRowView[0],
                    new TableDetailsRow[0]);

                var storeModelBuilder = StoreModelBuilderTests.CreateStoreModelBuilder(
                    "System.Data.SqlClient",
                    "2008",
                    EntityFrameworkVersion.Version1);

                var storeModel = storeModelBuilder.Build(storeSchemaDetails);

                var mappingContext = CreateOneToOneMappingBuilder().Build(storeModel);

                Assert.Equal(1, mappingContext.ConceptualAssociationTypes().Count());

                var associationType = mappingContext.ConceptualAssociationTypes().ElementAt(0);

                Assert.False(associationType.IsForeignKey);
                Assert.Equal(1, associationType.ReferentialConstraints.Count);

                Assert.Equal(
                    new[] { mappingContext.StoreAssociationTypes().Single().ReferentialConstraints[0].ToProperties[0] },
                    mappingContext.StoreForeignKeyProperties);
            }
            public static void CreateCollapsibleItems_does_not_create_collapsible_item_if_IsAtLeastOneColumnFkInBothAssociations()
            {
                var tableDetails = new[]
                    {
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "A", "Id", 0, false, "int", isIdentiy: true, isPrimaryKey: true),
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "A", "Col1", 1, false, "int", isIdentiy: false, isPrimaryKey: true),
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "B", "Col1", 0, false, "int", isIdentiy: false, isPrimaryKey: true),
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "B", "Col2", 1, false, "int", isIdentiy: false, isPrimaryKey: true),
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "C", "Id", 0, false, "int", isIdentiy: true, isPrimaryKey: true),
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "C", "Col1", 1, false, "int", isIdentiy: false, isPrimaryKey: true),
                        StoreModelBuilderTests.CreateRow(
                            "catalog", "schema", "C", "Col2", 2, false, "int", isIdentiy: false, isPrimaryKey: true)
                    };

                var relationshipDetails = new[]
                    {
                        StoreModelBuilderTests.CreateRelationshipDetailsRow(
                            "R1", "R1", 0, false, "catalog", "schema", "A", "Id", "catalog", "schema", "C", "Id"),
                        StoreModelBuilderTests.CreateRelationshipDetailsRow(
                            "R1", "R1", 1, false, "catalog", "schema", "A", "Col1", "catalog", "schema", "C", "Col1"),
                        StoreModelBuilderTests.CreateRelationshipDetailsRow(
                            "R2", "R2", 0, false, "catalog", "schema", "B", "Col1", "catalog", "schema", "C", "Col1"),
                        StoreModelBuilderTests.CreateRelationshipDetailsRow(
                            "R2", "R2", 1, false, "catalog", "schema", "B", "Col2", "catalog", "schema", "C", "Col2")
                    };

                var storeSchemaDetails = new StoreSchemaDetails(
                    tableDetails,
                    new TableDetailsRow[0],
                    relationshipDetails,
                    new FunctionDetailsRowView[0],
                    new TableDetailsRow[0]);

                var storeModelBuilder = StoreModelBuilderTests.CreateStoreModelBuilder(
                    "System.Data.SqlClient",
                    "2008",
                    null,
                    "myModel",
                    generateForeignKeyProperties: true);

                var storeModel = storeModelBuilder.Build(storeSchemaDetails);

                IEnumerable<AssociationSet> associationSetsFromNonCollapsibleItems;
                var collapsibleItems = CollapsibleEntityAssociationSets.CreateCollapsibleItems(
                    storeModel.Containers.Single().BaseEntitySets,
                    out associationSetsFromNonCollapsibleItems);

                Assert.Equal(0, collapsibleItems.Count());
                Assert.Equal(2, associationSetsFromNonCollapsibleItems.Count());
                Assert.Equal("R1", associationSetsFromNonCollapsibleItems.ElementAtOrDefault(0).Name);
                Assert.Equal("R2", associationSetsFromNonCollapsibleItems.ElementAtOrDefault(1).Name);
            }
            private static EdmModel BuildStoreModel(
                TableDetailsRow[] tableDetails,
                RelationshipDetailsRow[] relationshipDetails)
            {
                var storeSchemaDetails = new StoreSchemaDetails(
                    tableDetails,
                    new TableDetailsRow[0],
                    relationshipDetails,
                    new FunctionDetailsRowView[0],
                    new TableDetailsRow[0]);

                var storeModelBuilder = StoreModelBuilderTests.CreateStoreModelBuilder();

                return storeModelBuilder.Build(storeSchemaDetails);
            }