Example #1
0
        public void GenerateModel_genertes_model_and_sets_all_the_properties()
        {
            var mockModelGenerator = new Mock <ModelGenerator>(new ModelBuilderSettings(), "storeNamespace");

            var storeModel     = new EdmModel(DataSpace.SSpace);
            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(
                storeModel.Containers.Single(),
                EntityContainer.Create("C", DataSpace.CSpace, null, null, null));

            mockModelGenerator
            .Setup(g => g.CreateStoreModel())
            .Returns(() => storeModel);
            mockModelGenerator
            .Setup(g => g.CreateMappingContext(It.Is <EdmModel>(model => model == storeModel)))
            .Returns(() => mappingContext);

            var errors          = new List <EdmSchemaError>();
            var databaseMapping = mockModelGenerator.Object.GenerateModel(errors).DatabaseMapping;

            Assert.Same(storeModel, databaseMapping.Database);
            Assert.NotNull(databaseMapping.Model);
            Assert.Equal(1, databaseMapping.EntityContainerMappings.Count);
            mockModelGenerator.Verify(
                g => g.CreateMappingContext(It.IsAny <EdmModel>()), Times.Once());
            Assert.Empty(errors);
        }
        public void BuildEntitySetMapping_creates_valid_entity_set_mapping()
        {
            var storeEntityType =
                EntityType.Create("foo_S", "bar_S", DataSpace.SSpace, null, null, null);

            var modelEntityType =
                EntityType.Create("foo_C", "bar_C", DataSpace.CSpace, null, null, null);

            var storeEntitySet = EntitySet.Create("ES_S", "Ns_S", null, null, storeEntityType, null);
            var storeContainer = EntityContainer.Create("C_S", DataSpace.SSpace, new[] { storeEntitySet }, null, null);
            var modelEntitySet = EntitySet.Create("ES_C", "Ns_C", null, null, storeEntityType, null);
            var modelContainer = EntityContainer.Create("C_C", DataSpace.SSpace, new[] { modelEntitySet }, null, null);

            var storageContainerMapping =
                new EntityContainerMapping(modelContainer, storeContainer, null, false, false);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);

            mappingContext.AddMapping(storeEntitySet, modelEntitySet);
            mappingContext.AddMapping(storeEntityType, modelEntityType);

            var entitySetMapping =
                DbDatabaseMappingBuilder.BuildEntitySetMappings(storageContainerMapping, mappingContext).Single();

            Assert.Same(modelEntitySet, entitySetMapping.EntitySet);
            Assert.Equal(1, entitySetMapping.EntityTypeMappings.Count());
        }
        public void Build_builds_valid_DbDatabaseMapping_for_entity_types()
        {
            var storeEntityType =
                EntityType.Create("foo_S", "bar_S", DataSpace.SSpace, null, null, null);

            var modelEntityType =
                EntityType.Create("foo_C", "bar_C", DataSpace.CSpace, null, null, null);

            var storeEntitySet = EntitySet.Create("ES_S", "Ns_S", null, null, storeEntityType, null);
            var storeContainer = EntityContainer.Create("C_S", DataSpace.SSpace, new[] { storeEntitySet }, null, null);
            var storeModel     = EdmModel.CreateStoreModel(storeContainer, null, null);

            var modelEntitySet = EntitySet.Create("ES_C", "Ns_C", null, null, storeEntityType, null);
            var modelContainer = EntityContainer.Create("C_C", DataSpace.CSpace, new[] { modelEntitySet }, null, null);

            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(storeContainer, modelContainer);
            mappingContext.AddMapping(storeEntitySet, modelEntitySet);
            mappingContext.AddMapping(storeEntityType, modelEntityType);

            var dbMapping = DbDatabaseMappingBuilder.Build(mappingContext).DatabaseMapping;

            Assert.Same(storeModel, dbMapping.Database);
            var entityContainerMapping = dbMapping.EntityContainerMappings.Single();

            Assert.Same(storeContainer, entityContainerMapping.StorageEntityContainer);
            Assert.Same(modelContainer, entityContainerMapping.EdmEntityContainer);
            Assert.Equal(1, entityContainerMapping.EntitySetMappings.Count());

            Assert.NotNull(dbMapping.Model);
            Assert.Same(modelContainer, dbMapping.Model.Containers.Single());
            Assert.Same(modelEntityType, dbMapping.Model.EntityTypes.Single());
        }
Example #4
0
        public static EntitySetBase CreateEntitySet(string containerName, string entitySetName, string entityTypeName = "E", string modelNamespace = "Model")
        {
            var entity    = EntityType.Create(entityTypeName, modelNamespace, DataSpace.CSpace, new string[0], new EdmMember[0], null);
            var entitySet = EntitySet.Create(entitySetName, modelNamespace, null, null, entity, null);

            EntityContainer.Create(containerName, DataSpace.CSpace, new[] { entitySet }, null, null);

            return(entitySet);
        }
        private static SimpleMappingContext CreateSimpleMappingContext(bool isForeignKey)
        {
            var int32TypeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            var storeP1        = new[] { CreateStoreProperty("storeSourceId", "int") };
            var storeP2        = new[] { CreateStoreProperty("storeTargetId", "int") };
            var modelP1        = new[] { EdmProperty.Create("modelSourceId", int32TypeUsage) };
            var modelP2        = new[] { EdmProperty.Create("modelTargetId", int32TypeUsage) };
            var storeET1       = EntityType.Create("storeET1", "N", DataSpace.SSpace, new[] { "storeSourceId" }, storeP1, null);
            var storeET2       = EntityType.Create("storeET2", "N", DataSpace.SSpace, new[] { "storeTargetId" }, storeP2, null);
            var modelET1       = EntityType.Create("modelET1", "N", DataSpace.CSpace, new[] { "modelSourceId" }, modelP1, null);
            var modelET2       = EntityType.Create("modelET2", "N", DataSpace.CSpace, new[] { "modelTargetId" }, modelP2, null);
            var storeES1       = EntitySet.Create("storeES1", null, null, null, storeET1, null);
            var storeES2       = EntitySet.Create("storeES2", null, null, null, storeET2, null);
            var modelES1       = EntitySet.Create("modelES1", null, null, null, modelET1, null);
            var modelES2       = EntitySet.Create("modelES2", null, null, null, modelET2, null);
            var storeEM1       = AssociationEndMember.Create(
                "storeEM1", storeET1.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null);
            var storeEM2 = AssociationEndMember.Create(
                "storeEM2", storeET2.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null);
            var modelEM1 = AssociationEndMember.Create(
                "modelEM1", modelET1.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null);
            var modelEM2 = AssociationEndMember.Create(
                "modelEM2", modelET2.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null);
            var storeRC        = new ReferentialConstraint(storeEM1, storeEM2, storeP1, storeP2);
            var modelRC        = new ReferentialConstraint(modelEM1, modelEM2, modelP1, modelP2);
            var storeAT        = AssociationType.Create("storeAT", "N", isForeignKey, DataSpace.SSpace, storeEM1, storeEM2, storeRC, null);
            var modelAT        = AssociationType.Create("modelAT", "N", isForeignKey, DataSpace.CSpace, modelEM1, modelEM2, modelRC, null);
            var storeAS        = AssociationSet.Create("storeAS", storeAT, storeES1, storeES2, null);
            var modelAS        = AssociationSet.Create("modelAS", modelAT, modelES1, modelES2, null);
            var storeContainer = EntityContainer.Create(
                "storeContainer", DataSpace.SSpace, new EntitySetBase[] { storeES1, storeES2, storeAS }, null, null);
            var modelContainer = EntityContainer.Create(
                "modelContainer", DataSpace.CSpace, new EntitySetBase[] { modelES1, modelES2, modelAS }, null, null);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);

            mappingContext.AddMapping(storeP1[0], modelP1[0]);
            mappingContext.AddMapping(storeP2[0], modelP2[0]);
            mappingContext.AddMapping(storeET1, modelET1);
            mappingContext.AddMapping(storeET2, modelET2);
            mappingContext.AddMapping(storeES1, modelES1);
            mappingContext.AddMapping(storeES2, modelES2);
            mappingContext.AddMapping(storeEM1, modelEM1);
            mappingContext.AddMapping(storeEM2, modelEM2);
            mappingContext.AddMapping(storeAT, modelAT);
            mappingContext.AddMapping(storeAS, modelAS);
            mappingContext.AddMapping(storeAS.AssociationSetEnds[0], modelAS.AssociationSetEnds[0]);
            mappingContext.AddMapping(storeAS.AssociationSetEnds[1], modelAS.AssociationSetEnds[1]);
            mappingContext.AddMapping(storeContainer, modelContainer);

            return(mappingContext);
        }
        public void Can_add_and_get_entity_container_mapping()
        {
            var es1 = EntitySet.Create("es1", null, null, null, CreateEntityType("e"), null);
            var ec1 = EntityContainer.Create("ec1", DataSpace.CSpace, new[] { es1 }, null, null);

            var es2 = EntitySet.Create("es1", null, null, null, CreateEntityType("e"), null);
            var ec2 = EntityContainer.Create("ec2", DataSpace.CSpace, new[] { es2 }, null, null);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);

            mappingContext.AddMapping(ec1, ec2);
            Assert.Same(ec2, mappingContext[ec1]);
        }
Example #7
0
        public void Mapping_context_created_with_CreateMappingContext_will_not_pluralize_if_pluralization_disabled()
        {
            var entityType = EntityType.Create("entities", "ns", DataSpace.SSpace, new string[0], new EdmMember[0], null);
            var entitySet  = EntitySet.Create("entitySet", "dbo", "t", null, entityType, null);
            var container  = EntityContainer.Create("container", DataSpace.SSpace, new[] { entitySet }, null, null);

            var storeModel = EdmModel.CreateStoreModel(container, null, null);

            var mappingContext =
                new ModelGenerator(new ModelBuilderSettings {
                UsePluralizationService = false
            }, "storeNamespace")
                .CreateMappingContext(storeModel);

            Assert.Equal("entities", mappingContext[entityType].Name);
            Assert.Equal("entitySet", mappingContext[entitySet].Name);
        }
        public void BuildEntityMapping_creates_valid_entity_mappings()
        {
            var storeEntityType =
                EntityType.Create(
                    "foo_S",
                    "bar_S",
                    DataSpace.SSpace,
                    new[] { "Id" },
                    new[] { CreateStoreProperty("Id", "int") },
                    null);

            var modelEntityType =
                EntityType.Create(
                    "foo_C",
                    "bar_C",
                    DataSpace.CSpace,
                    new[] { "C_Id" },
                    new[] { EdmProperty.CreatePrimitive("C_Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)) },
                    null);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);

            mappingContext.AddMapping(storeEntityType, modelEntityType);
            mappingContext.AddMapping(storeEntityType.Properties.Single(), modelEntityType.Properties.Single());

            var modelEntitySet       = EntitySet.Create("ES", "Ns", null, null, modelEntityType, null);
            var modelEntityContainer = EntityContainer.Create("C", DataSpace.SSpace, new[] { modelEntitySet }, null, null);

            var storeEntitySet          = EntitySet.Create("ES", "Ns.Store", null, null, storeEntityType, null);
            var storageEntitySetMapping =
                new EntitySetMapping(
                    modelEntitySet,
                    new EntityContainerMapping(modelEntityContainer, null, null, false, false));

            var typeMapping =
                DbDatabaseMappingBuilder
                .BuildEntityTypeMapping(storageEntitySetMapping, mappingContext, storeEntitySet);

            Assert.Same(modelEntityType, typeMapping.EntityType);
            Assert.Equal(1, typeMapping.MappingFragments.Count);
            var mappingFragment = typeMapping.MappingFragments.Single();

            Assert.Equal(storeEntityType, mappingFragment.Table);
            Assert.Equal(1, mappingFragment.ColumnMappings.Count());
        }
Example #9
0
        public void GenerateModel_combines_store_model_and_mapping_errors()
        {
            var storeModelError       = new EdmSchemaError("storeError", 42, EdmSchemaErrorSeverity.Error);
            var errorMetadataProperty =
                MetadataProperty.Create(
                    MetadataItemHelper.SchemaErrorsMetadataPropertyName,
                    TypeUsage.CreateDefaultTypeUsage(
                        PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String).GetCollectionType()),
                    new List <EdmSchemaError> {
                storeModelError
            });

            var entityType =
                EntityType.Create(
                    "foo", "bar", DataSpace.SSpace, new string[0], new EdmMember[0],
                    new[] { errorMetadataProperty });

            var storeModel = new EdmModel(DataSpace.SSpace);

            storeModel.AddItem(entityType);

            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(
                storeModel.Containers.Single(),
                EntityContainer.Create("C", DataSpace.CSpace, null, null, null));
            mappingContext.Errors.Add(new EdmSchemaError("mappingError", 911, EdmSchemaErrorSeverity.Warning));

            var mockModelGenerator = new Mock <ModelGenerator>(new ModelBuilderSettings(), "storeNamespace");

            mockModelGenerator
            .Setup(g => g.CreateStoreModel())
            .Returns(() => storeModel);

            mockModelGenerator
            .Setup(g => g.CreateMappingContext(It.IsAny <EdmModel>()))
            .Returns(() => mappingContext);

            var errors = new List <EdmSchemaError>();

            mockModelGenerator.Object.GenerateModel(errors);
            Assert.Equal(new[] { storeModelError, mappingContext.Errors.Single() }, errors);
        }
Example #10
0
        public void Affected_entity_sets_discovered_for_modification_commands()
        {
            var entityType = EntityType.Create("e", "ns", DataSpace.CSpace, new string[0], new EdmMember[0], null);
            var entitySet  = EntitySet.Create("es", "ns", null, null, entityType, null);

            EntityContainer.Create("ec", DataSpace.CSpace, new[] { entitySet }, null, null);

            var commandTreeFacts =
                new CommandTreeFacts(
                    new DbInsertCommandTree(
                        new MetadataWorkspace(),
                        DataSpace.CSpace,
                        entitySet.Scan().Bind(),
                        new List <DbModificationClause>().AsReadOnly(),
                        TypeUsage.CreateDefaultTypeUsage(entityType).Null()));

            Assert.Equal(
                new[] { "es" },
                commandTreeFacts.AffectedEntitySets.Select(s => s.Name));
        }
        public void WriteContainer_writes_annotation_namespace_when_requested()
        {
            var container = EntityContainer.Create(
                "C", DataSpace.CSpace, new EntitySetBase[0], null,
                new[]
            {
                new MetadataProperty(
                    "http://schemas.microsoft.com/ado/2009/02/edm/annotation:LazyLoadingEnabled",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)), "true")
            });

            var fixture = new Fixture();

            // WriteSchemaElementHeader binds the "annotation" namespace prefix to the annotation namespace uri
            fixture.Writer.WriteSchemaElementHeader("ns");
            fixture.Writer.WriteEntityContainerElementHeader(container);

            Assert.Equal(
                @"<Schema Namespace=""ns"" Alias=""Self"" annotation:UseStrongSpatialTypes=""false"" xmlns:annotation=""http://schemas.microsoft.com/ado/2009/02/edm/annotation"" xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"">" +
                @"<EntityContainer Name=""C"" annotation:LazyLoadingEnabled=""true""",
                fixture.ToString());
        }
        public void Build_adds_association_types_to_model()
        {
            var storeEntityType =
                EntityType.Create("foo_S", "bar_S", DataSpace.SSpace, null, null, null);
            var storeEntitySet = EntitySet.Create("ES_S", "Ns_S", null, null, storeEntityType, null);
            var storeContainer = EntityContainer.Create("C_S", DataSpace.SSpace, new[] { storeEntitySet }, null, null);
            var storeModel     = EdmModel.CreateStoreModel(storeContainer, null, null);

            var conceptualAssociationType =
                AssociationType.Create("AT_C", "ns", false, DataSpace.CSpace, null, null, null, null);
            var associationSet = AssociationSet.Create("AS_C", conceptualAssociationType, null, null, null);
            var modelContainer = EntityContainer.Create("C_C", DataSpace.CSpace, new[] { associationSet }, null, null);

            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(storeContainer, modelContainer);
            mappingContext.AddMapping(new CollapsibleEntityAssociationSets(storeEntitySet), associationSet);

            var model = DbDatabaseMappingBuilder.Build(mappingContext);

            Assert.Same(conceptualAssociationType, model.ConceptualModel.AssociationTypes.SingleOrDefault());
        }
Example #13
0
        public SimpleMappingContext Build(EdmModel storeModel)
        {
            Debug.Assert(storeModel != null, "storeModel != null");

            var mappingContext = new SimpleMappingContext(storeModel, _generateForeignKeyProperties);

            var uniqueEntityContainerNames = new UniqueIdentifierService();
            var globallyUniqueTypeNames    = new UniqueIdentifierService();

            CollectForeignKeyProperties(mappingContext, storeModel);

            foreach (var storeEntitySet in storeModel.Containers.Single().EntitySets)
            {
                GenerateEntitySet(mappingContext, storeEntitySet, uniqueEntityContainerNames, globallyUniqueTypeNames);
            }

            GenerateAssociationSets(
                mappingContext,
                uniqueEntityContainerNames,
                globallyUniqueTypeNames);

            var functionImports =
                GenerateFunctions(mappingContext, storeModel, uniqueEntityContainerNames, globallyUniqueTypeNames)
                .ToArray();

            var conceptualModelContainer = EntityContainer.Create(
                _containerName,
                DataSpace.CSpace,
                mappingContext.ConceptualEntitySets()
                .Concat(mappingContext.ConceptualAssociationSets().Cast <EntitySetBase>()),
                functionImports,
                EntityFrameworkVersion.DoubleToVersion(storeModel.SchemaVersion) >= EntityFrameworkVersion.Version2
                    ? new[] { CreateAnnotationMetadataProperty("LazyLoadingEnabled", "true") }
                    : null);

            mappingContext.AddMapping(storeModel.Containers.Single(), conceptualModelContainer);

            return(mappingContext);
        }
        public void Build_does_not_try_map_not_mapped_functions()
        {
            var rowTypeProperty = CreateStoreProperty("p1", "int");
            var storeFunction   = EdmFunction.Create(
                "f_s",
                "storeModel",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        RowType.Create(new[] { rowTypeProperty }, null).GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                null);

            var modelContainer = EntityContainer.Create("C_C", DataSpace.CSpace, new EntitySet[0], null, null);
            var storeContainer = EntityContainer.Create("C_S", DataSpace.SSpace, new EntitySet[0], null, null);

            var storeModel = EdmModel.CreateStoreModel(storeContainer, null, null);

            storeModel.AddItem(storeFunction);

            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(storeContainer, modelContainer);

            var entityModel = DbDatabaseMappingBuilder.Build(mappingContext).ConceptualModel;

            Assert.NotNull(entityModel);
            Assert.Empty(entityModel.Containers.Single().FunctionImports);
        }
Example #15
0
        public void Affected_entity_sets_and_functions_discovered_for_queries()
        {
            var e1  = EntityType.Create("e1", "ns", DataSpace.CSpace, new string[0], new EdmMember[0], null);
            var e2  = EntityType.Create("e2", "ns", DataSpace.CSpace, new string[0], new EdmMember[0], null);
            var es1 = EntitySet.Create("es1", "ns", null, null, e1, null);
            var es2 = EntitySet.Create("es2", "ns", null, null, e2, null);

            EntityContainer.Create("ec", DataSpace.CSpace, new[] { es1, es2 }, null, null);

            var commandTreeFacts =
                new CommandTreeFacts(
                    new DbQueryCommandTree(
                        new MetadataWorkspace(),
                        DataSpace.CSpace,
                        es1.Scan().Exists().And(es2.Scan().Exists()),
                        validate: false));

            Assert.Equal(
                new[] { "es1", "es2" },
                commandTreeFacts.AffectedEntitySets.Select(s => s.Name));

            Assert.False(commandTreeFacts.UsesNonDeterministicFunctions);
        }
Example #16
0
        internal static MetadataWorkspace CreateTransientMetadataWorkspace(
            IList <EntitySet> sourceEntitySets,
            Version targetEntityFrameworkVersion,
            string providerInvariantName,
            string providerManifestToken,
            DbProviderManifest providerManifest)
        {
            Debug.Assert(sourceEntitySets != null, "sourceEntitySets != null");
            Debug.Assert(targetEntityFrameworkVersion != null, "targetEntityFrameworkVersion != null");
            Debug.Assert(!string.IsNullOrWhiteSpace(providerInvariantName), "invalid providerInvariantName");
            Debug.Assert(!string.IsNullOrWhiteSpace(providerManifestToken), "invalid providerManifestToken");
            Debug.Assert(providerManifest != null, "providerManifest != null");

            var targetDoubleEntityFrameworkVersion = EntityFrameworkVersion.VersionToDouble(targetEntityFrameworkVersion);

            var storeModel =
                EdmModel.CreateStoreModel(
                    EntityContainer.Create(
                        StoreContainerName,
                        DataSpace.SSpace,
                        sourceEntitySets,
                        Enumerable.Empty <EdmFunction>(),
                        null),
                    new DbProviderInfo(providerInvariantName, providerManifestToken),
                    providerManifest,
                    targetDoubleEntityFrameworkVersion);

            foreach (var entityType in sourceEntitySets.Select(e => e.ElementType))
            {
                storeModel.AddItem(entityType);
            }

            var storeItemCollection = new StoreItemCollection(storeModel);

            var edmItemCollection =
                new EdmItemCollection(
                    EdmModel.CreateConceptualModel(
                        EntityContainer.Create(
                            ModelContainerName,
                            DataSpace.CSpace,
                            Enumerable.Empty <EntitySet>(),
                            Enumerable.Empty <EdmFunction>(),
                            null),
                        targetDoubleEntityFrameworkVersion));

            var msl =
                string.Format(
                    CultureInfo.InvariantCulture,
                    MslTemplate,
                    SchemaManager.GetMSLNamespaceName(targetEntityFrameworkVersion));

            StorageMappingItemCollection mappingItemCollection;

            using (var stringReader = new StringReader(msl))
            {
                using (var reader = XmlReader.Create(stringReader))
                {
                    mappingItemCollection = new StorageMappingItemCollection(edmItemCollection, storeItemCollection, new[] { reader });
                }
            }

            return(new MetadataWorkspace(
                       () => edmItemCollection,
                       () => storeItemCollection,
                       () => mappingItemCollection));
        }
        public void Build_builds_valid_DbDatabaseMapping_for_functions()
        {
            var rowTypeProperty = CreateStoreProperty("p1", "int");

            var complexTypeProperty =
                EdmProperty.Create(
                    "p2",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));

            var functionImportReturnComplexType =
                ComplexType.Create(
                    "CT",
                    "entityModel",
                    DataSpace.CSpace,
                    new[] { complexTypeProperty }, null);

            var storeFunction = EdmFunction.Create(
                "f_s",
                "storeModel",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        RowType.Create(new[] { rowTypeProperty }, null).GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                null);

            var functionImport =
                EdmFunction.Create(
                    "f_c",
                    "entityModel",
                    DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        functionImportReturnComplexType.GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                    null);

            var modelContainer = EntityContainer.Create("C_C", DataSpace.CSpace, new EntitySet[0], new[] { functionImport }, null);
            var storeContainer = EntityContainer.Create("C_S", DataSpace.SSpace, new EntitySet[0], null, null);

            var storeModel = EdmModel.CreateStoreModel(storeContainer, null, null);

            storeModel.AddItem(storeFunction);

            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(rowTypeProperty, complexTypeProperty);
            mappingContext.AddMapping(storeFunction, functionImport);
            mappingContext.AddMapping(storeContainer, modelContainer);

            var entityModel = DbDatabaseMappingBuilder.Build(mappingContext).ConceptualModel;

            Assert.NotNull(entityModel);
            Assert.Equal(new[] { "f_c" }, entityModel.Containers.Single().FunctionImports.Select(f => f.Name));
            Assert.Equal(new[] { "CT" }, entityModel.ComplexTypes.Select(t => t.Name));
        }
        public void BuildAssociationSetMappings_builds_conceptual_association_set_mapping_for_collapsed_store_entity_sets()
        {
            #region Setting up many to many relationship in the SSpace Teacher * -- 1 TeacherStudents 1 -- * Teachers

            var joinStoreEntityType =
                EntityType.Create(
                    "TeacherStudents", "ns.Store", DataSpace.SSpace,
                    new[] { "JoinTeacherId", "JoinStudentId" },
                    new[]
            {
                CreateStoreProperty("JoinTeacherId", "int"),
                CreateStoreProperty("JoinStudentId", "int")
            }, null);

            var joinStoreEntitySet =
                EntitySet.Create("TeacherStudentsSet", "dbo", "TeacherStudentTable", null, joinStoreEntityType, null);

            var storeTeacherEntityType =
                EntityType.Create(
                    "Teacher", "ns.Store", DataSpace.SSpace, new[] { "TeacherId" },
                    new[] { CreateStoreProperty("TeacherId", "int") }, null);
            var storeTeacherEntitySet =
                EntitySet.Create("TeachersSet", "dbo", "Teachers", null, storeTeacherEntityType, null);

            var storeStudentEntityType =
                EntityType.Create(
                    "Student", "ns.Store", DataSpace.SSpace, new[] { "StudentId" },
                    new[] { CreateStoreProperty("StudentId", "int") }, null);
            var storeStudentEntitySet =
                EntitySet.Create("StudentSet", "dbo", "Students", null, storeStudentEntityType, null);

            var storeTeachersEndMember =
                AssociationEndMember.Create(
                    "Teachers", storeTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var storeTeacherStudentsfromTeachersEndMember =
                AssociationEndMember.Create(
                    "TeacherStudents_fromTeachers", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One,
                    OperationAction.None, null);

            var storeTeacherAssociationType =
                AssociationType.Create(
                    "Teacher_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace,
                    storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember,
                    new ReferentialConstraint(
                        storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember, storeTeacherEntityType.KeyProperties,
                        joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinTeacherId")),
                    null);

            var storeTeacherAssociationSet =
                AssociationSet.Create(
                    "Teacher_TeacherStudents", storeTeacherAssociationType, storeTeacherEntitySet, joinStoreEntitySet, null);

            var storeStudentsEndMember =
                AssociationEndMember.Create(
                    "Students", storeStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var storeTeacherStudentsfromStudentsEndMember =
                AssociationEndMember.Create(
                    "TeacherStudents_fromStudents", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One,
                    OperationAction.None, null);

            var storeStudentAssociationType =
                AssociationType.Create(
                    "Student_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace,
                    storeStudentsEndMember,
                    storeTeacherStudentsfromStudentsEndMember,
                    new ReferentialConstraint(
                        storeStudentsEndMember, storeTeacherStudentsfromStudentsEndMember, storeStudentEntityType.KeyProperties,
                        joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinStudentId")),
                    null);

            var storeStudentAssociationSet =
                AssociationSet.Create(
                    "Student_TeacherStudents", storeStudentAssociationType, storeStudentEntitySet, joinStoreEntitySet, null);

            var collapsedAssociationSet = new CollapsibleEntityAssociationSets(joinStoreEntitySet);
            collapsedAssociationSet.AssociationSets.Add(storeTeacherAssociationSet);
            collapsedAssociationSet.AssociationSets.Add(storeStudentAssociationSet);

            #endregion

            #region Setting up many to many relationship in the CSpace Teacher * -- * Teachers

            var conceptualContainer = EntityContainer.Create("ConceptualContainer", DataSpace.CSpace, null, null, null);

            var edmIntTypeUsage =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var conceptualTeacherEntityType =
                EntityType.Create(
                    "Teacher", "ns", DataSpace.CSpace, new[] { "TeacherId" },
                    new[] { EdmProperty.Create("TeacherId", edmIntTypeUsage) }, null);

            var conceptualTeacherEntitySet =
                EntitySet.Create("TeachersSet", null, null, null, conceptualTeacherEntityType, null);

            var conceptualStudentEntityType =
                EntityType.Create(
                    "Student", "ns", DataSpace.CSpace, new[] { "StudentId" },
                    new[] { EdmProperty.Create("StudentId", edmIntTypeUsage) }, null);

            var conceptualStudentEntitySet =
                EntitySet.Create("StudentSet", "dbo", "Students", null, conceptualStudentEntityType, null);

            var conceptualTeachersEndMember =
                AssociationEndMember.Create(
                    "TeachersEnd", conceptualTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var conceptualStudentsEndMember =
                AssociationEndMember.Create(
                    "StudentsEnd", conceptualStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var conceptualAssociationType =
                AssociationType.Create(
                    "TeacherStudentAssociation",
                    "ns.Model",
                    false,
                    DataSpace.CSpace,
                    conceptualTeachersEndMember,
                    conceptualStudentsEndMember,
                    new ReferentialConstraint(
                        conceptualTeachersEndMember, conceptualStudentsEndMember,
                        conceptualTeacherEntityType.KeyProperties, conceptualStudentEntityType.KeyProperties),
                    null);

            var conceptualAssociationSet =
                AssociationSet.Create(
                    "TeacherStudentSet", conceptualAssociationType, conceptualTeacherEntitySet,
                    conceptualStudentEntitySet, null);

            #endregion

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            mappingContext.AddMapping(collapsedAssociationSet, conceptualAssociationSet);
            mappingContext.AddMapping(storeTeachersEndMember, conceptualTeachersEndMember);
            mappingContext.AddMapping(storeStudentsEndMember, conceptualStudentsEndMember);
            mappingContext.AddMapping(
                storeTeacherAssociationSet.AssociationSetEnds.ElementAt(0),
                conceptualAssociationSet.AssociationSetEnds.ElementAt(0));
            mappingContext.AddMapping(
                storeStudentAssociationSet.AssociationSetEnds.ElementAt(0),
                conceptualAssociationSet.AssociationSetEnds.ElementAt(1));
            mappingContext.AddMapping(
                storeStudentEntityType.KeyProperties.Single(), conceptualStudentEntityType.KeyProperties.Single());
            mappingContext.AddMapping(
                storeTeacherEntityType.KeyProperties.Single(), conceptualTeacherEntityType.KeyProperties.Single());

            var storageEntitySetMapping =
                new EntityContainerMapping(conceptualContainer, null, null, false, false);

            var associationSetMapping =
                DbDatabaseMappingBuilder.BuildAssociationSetMappings(storageEntitySetMapping, mappingContext)
                .SingleOrDefault();
            Assert.NotNull(associationSetMapping);

            var mappingFragment = associationSetMapping.TypeMappings.SingleOrDefault();
            Assert.NotNull(mappingFragment);

            var propertyMappings = mappingFragment.MappingFragments.Single().PropertyMappings;
            Assert.Equal(2, propertyMappings.Count);
            Assert.Same(conceptualTeachersEndMember, ((EndPropertyMapping)propertyMappings[0]).AssociationEnd);
            Assert.Same(conceptualStudentsEndMember, ((EndPropertyMapping)propertyMappings[1]).AssociationEnd);

            var scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[0]).PropertyMappings.Single();
            Assert.Same(conceptualTeacherEntityType.KeyMembers.Single(), scalarPropertyMapping.Property);
            Assert.Same(
                joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinTeacherId"),
                scalarPropertyMapping.Column);

            scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[1]).PropertyMappings.Single();
            Assert.Same(conceptualStudentEntityType.KeyMembers.Single(), scalarPropertyMapping.Property);
            Assert.Same(
                joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinStudentId"),
                scalarPropertyMapping.Column);
        }