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());
        }
        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);
        }
        private static EdmModel BuildEntityModel(SimpleMappingContext mappingContext)
        {
            var conceptualModelContainer = mappingContext[mappingContext.StoreModel.Containers.Single()];
            var entityModel = EdmModel.CreateConceptualModel(conceptualModelContainer, mappingContext.StoreModel.SchemaVersion);

            foreach (var entityType in mappingContext.ConceptualEntityTypes())
            {
                entityModel.AddItem(entityType);
            }

            foreach (var associationSet in mappingContext.ConceptualAssociationSets())
            {
                entityModel.AddItem(associationSet.ElementType);
            }

            foreach (var mappedStoredFunction in mappingContext.MappedStoreFunctions())
            {
                var functionImport = mappingContext[mappedStoredFunction];
                entityModel.AddItem(
                    (ComplexType)
                    ((CollectionType)functionImport.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType);
            }

            return entityModel;
        }
        private static EntityContainerMapping BuildEntityContainerMapping(SimpleMappingContext mappingContext)
        {
            var storeEntityContainer = mappingContext.StoreModel.Containers.Single();
            var entityContainerMapping =
                new EntityContainerMapping(
                    mappingContext[storeEntityContainer],
                    storeEntityContainer,
                    null,
                    false,
                    false);

            foreach (var entitySetMapping in BuildEntitySetMappings(entityContainerMapping, mappingContext))
            {
                entityContainerMapping.AddSetMapping(entitySetMapping);
            }

            foreach (var associationSetMapping in BuildAssociationSetMappings(entityContainerMapping, mappingContext))
            {
                entityContainerMapping.AddSetMapping(associationSetMapping);
            }

            foreach (var mappedStoredFunction in mappingContext.MappedStoreFunctions())
            {
                entityContainerMapping.AddFunctionImportMapping(
                    BuildComposableFunctionMapping(mappedStoredFunction, mappingContext));
            }

            return entityContainerMapping;
        }
 public void Store_model_initialized()
 {
     var storeModel = new EdmModel(DataSpace.SSpace);
     var mappingContext = new SimpleMappingContext(storeModel, true);
     Assert.Same(storeModel, mappingContext.StoreModel);
     Assert.True(mappingContext.IncludeForeignKeyProperties);
 }
        public void Can_add_and_get_property_mapping()
        {
            var p1 = EdmProperty.CreatePrimitive("p1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            var p2 = EdmProperty.CreatePrimitive("p2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            mappingContext.AddMapping(p1, p2);
            Assert.Same(p2, mappingContext[p1]);
        }
        public void Can_add_get_error()
        {
            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            Assert.Empty(mappingContext.Errors);

            var error = new EdmSchemaError("bar", 0xF00, EdmSchemaErrorSeverity.Warning);
            mappingContext.Errors.Add(error);

            Assert.Same(error, mappingContext.Errors.Single());
        }
        public void Can_add_and_get_entity_type_mapping()
        {
            var e1 = CreateEntityType("e1");
            var e2 = CreateEntityType("e2");

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            Assert.Empty(mappingContext.ConceptualEntityTypes());
            mappingContext.AddMapping(e1, e2);
            Assert.Same(e2, mappingContext[e1]);
            Assert.Same(e2, mappingContext.ConceptualEntityTypes().Single());
        }
        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]);
        }
        public static DbModel Build(SimpleMappingContext mappingContext)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var databaseMapping =
                new DbDatabaseMapping
                    {
                        Database = mappingContext.StoreModel,
                        Model = BuildEntityModel(mappingContext)
                    };

            databaseMapping.AddEntityContainerMapping(BuildEntityContainerMapping(mappingContext));

            return new DbModel(databaseMapping, new DbModelBuilder());
        }
        public void Can_add_and_get_entity_set_mapping()
        {
            var dummy = CreateEntityType("e");
            var es1 = EntitySet.Create("es1", null, null, null, dummy, null);
            var es2 = EntitySet.Create("es2", null, null, null, dummy, null);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            mappingContext.AddMapping(es1, es2);
            Assert.Same(es2, mappingContext[es1]);

            EntitySet outEntitySet;

            Assert.True(mappingContext.TryGetValue(es1, out outEntitySet));
            Assert.Same(es2, outEntitySet);

            Assert.False(mappingContext.TryGetValue(es2, out outEntitySet));
            Assert.Null(outEntitySet);
        }
        private static AssociationEndMember GenerateAssociationEndMember(
            SimpleMappingContext mappingContext,
            AssociationEndMember storeEndMember,
            UniqueIdentifierService uniqueEndMemberNames,
            RelationshipMultiplicity multiplicity,
            OperationAction deleteBehavior)
        {
            var storeEntityType      = ((EntityType)((RefType)storeEndMember.TypeUsage.EdmType).ElementType);
            var conceptualEntityType = mappingContext[storeEntityType];

            var conceptualEndMember = AssociationEndMember.Create(
                CreateModelName(storeEndMember.Name, uniqueEndMemberNames),
                conceptualEntityType.GetReferenceType(),
                multiplicity,
                deleteBehavior,
                null);

            mappingContext.AddMapping(storeEndMember, conceptualEndMember);

            return(conceptualEndMember);
        }
        // internal for testing
        internal void GenerateEntitySet(
            SimpleMappingContext mappingContext,
            EntitySet storeEntitySet,
            UniqueIdentifierService uniqueEntityContainerNames,
            UniqueIdentifierService globallyUniqueTypeNames)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(storeEntitySet != null, "storeEntitySet != null");
            Debug.Assert(uniqueEntityContainerNames != null, "uniqueEntityContainerNames != null");
            Debug.Assert(globallyUniqueTypeNames != null, "globallyUniqueTypeNames != null");

            var conceptualEntityType = GenerateEntityType(mappingContext, storeEntitySet.ElementType, globallyUniqueTypeNames);

            var conceptualEntitySetName = CreateModelName(
                (_pluralizationService != null) ? _pluralizationService.Pluralize(storeEntitySet.Name) : storeEntitySet.Name,
                uniqueEntityContainerNames);

            var conceptualEntitySet = EntitySet.Create(conceptualEntitySetName, null, null, null, conceptualEntityType, null);

            mappingContext.AddMapping(storeEntitySet, conceptualEntitySet);
        }
Example #14
0
        // internal for testing
        internal void GenerateEntitySet(
            SimpleMappingContext mappingContext,
            EntitySet storeEntitySet,
            UniqueIdentifierService uniqueEntityContainerNames,
            UniqueIdentifierService globallyUniqueTypeNames)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(storeEntitySet != null, "storeEntitySet != null");
            Debug.Assert(uniqueEntityContainerNames != null, "uniqueEntityContainerNames != null");
            Debug.Assert(globallyUniqueTypeNames != null, "globallyUniqueTypeNames != null");

            var conceptualEntityType = GenerateEntityType(mappingContext, storeEntitySet.ElementType, globallyUniqueTypeNames);

            var conceptualEntitySetName = CreateModelName(
                (_pluralizationService != null) ? _pluralizationService.Pluralize(storeEntitySet.Name) : storeEntitySet.Name,
                uniqueEntityContainerNames);

            var conceptualEntitySet = EntitySet.Create(conceptualEntitySetName, null, null, null, conceptualEntityType, null);

            mappingContext.AddMapping(storeEntitySet, conceptualEntitySet);
        }
        public void Can_add_get_association_type_mapping()
        {
            var storeAssociationType =
                AssociationType.Create("storeAssociationType", "ns.Store", false, DataSpace.SSpace, null, null, null, null);
            var conceptualAssociationType =
                AssociationType.Create("conceptualAssociationType", "ns", false, DataSpace.CSpace, null, null, null, null);

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

            mappingContext.AddMapping(storeAssociationType, conceptualAssociationType);

            Assert.Same(conceptualAssociationType, mappingContext[storeAssociationType]);

            AssociationType outAssociationType;

            Assert.True(mappingContext.TryGetValue(storeAssociationType, out outAssociationType));
            Assert.Same(conceptualAssociationType, outAssociationType);

            Assert.False(mappingContext.TryGetValue(conceptualAssociationType, out outAssociationType));
            Assert.Null(outAssociationType);
        }
        public void Can_add_and_get_association_set_end_mapping()
        {
            var et1  = CreateEntityType("et1");
            var et2  = CreateEntityType("et2");
            var es1  = EntitySet.Create("es1", null, null, null, et1, null);
            var es2  = EntitySet.Create("es2", null, null, null, et2, null);
            var aem1 = AssociationEndMember.Create("aem1", et1.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null);
            var aem2 = AssociationEndMember.Create("aem2", et2.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null);
            var at1  = AssociationType.Create("at1", "ns", false, DataSpace.CSpace, aem1, aem2, null, null);
            var as1  = AssociationSet.Create("as1", at1, es1, es2, null);

            Assert.Equal(2, as1.AssociationSetEnds.Count);
            var ase1 = as1.AssociationSetEnds[0];
            var ase2 = as1.AssociationSetEnds[1];

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

            mappingContext.AddMapping(ase1, ase2);

            Assert.Same(ase2, mappingContext[ase1]);
        }
        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());
        }
        public void BuildPropertyMapping_does_not_build_property_mappings_for_foreign_key_properties_if_foreign_keys_disabled()
        {
            var storeEntityType =
                EntityType.Create(
                    "foo",
                    "bar",
                    DataSpace.SSpace,
                    new[] { "Id" },
                    new[]
            {
                CreateStoreProperty("Id", "int"),
                CreateStoreProperty("ForeignKey", "int"),
            },
                    null);

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

            mappingContext.StoreForeignKeyProperties.Add(storeEntityType.Properties.Single(p => p.Name == "ForeignKey"));

            foreach (var storeProperty in storeEntityType.Properties)
            {
                mappingContext.AddMapping(
                    storeProperty,
                    EdmProperty.CreatePrimitive(
                        storeProperty.Name + "Model",
                        (PrimitiveType)storeProperty.TypeUsage.EdmType));
            }

            var propertyMappings =
                DbDatabaseMappingBuilder
                .BuildPropertyMapping(storeEntityType, mappingContext);

            Assert.Equal(
                new[] { "Id" },
                propertyMappings.Select(m => m.ColumnProperty.Name));

            Assert.Equal(
                new[] { "IdModel" },
                propertyMappings.SelectMany(m => m.PropertyPath, (m, p) => p.Name));
        }
        public void BuildPropertyMapping_creates_valid_property_mappings()
        {
            var storeEntityType =
                EntityType.Create(
                    "foo",
                    "bar",
                    DataSpace.SSpace,
                    new[] { "Id" },
                    new[]
            {
                CreateStoreProperty("Id", "int"),
                CreateStoreProperty("FirstName", "nvarchar"),
                CreateStoreProperty("LastName", "char")
            },
                    null);

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

            foreach (var storeProperty in storeEntityType.Properties)
            {
                mappingContext.AddMapping(
                    storeProperty,
                    EdmProperty.CreatePrimitive(
                        storeProperty.Name + "Model",
                        (PrimitiveType)storeProperty.TypeUsage.EdmType));
            }

            var propertyMappings =
                DbDatabaseMappingBuilder
                .BuildPropertyMapping(storeEntityType, mappingContext);

            Assert.Equal(
                new[] { "Id", "FirstName", "LastName" },
                propertyMappings.Select(m => m.ColumnProperty.Name));

            Assert.Equal(
                new[] { "IdModel", "FirstNameModel", "LastNameModel" },
                propertyMappings.SelectMany(m => m.PropertyPath, (m, p) => p.Name));
        }
Example #20
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);
        }
Example #21
0
        BuildEntityTypeMapping(EntitySetMapping storeEntitySetMapping, SimpleMappingContext mappingContext, EntitySet storeEntitySet)
        {
            Debug.Assert(storeEntitySetMapping != null, "storeEntitySetMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var entityType = storeEntitySetMapping.EntitySet.ElementType;

            var entityTypeMapping = new EntityTypeMapping(storeEntitySetMapping);

            entityTypeMapping.AddType(entityType);

            var mappingFragment = new MappingFragment(storeEntitySet, entityTypeMapping, false);

            entityTypeMapping.AddFragment(mappingFragment);

            foreach (var propertyMapping in BuildPropertyMapping(storeEntitySet.ElementType, mappingContext))
            {
                mappingFragment.AddColumnMapping(propertyMapping);
            }

            return(entityTypeMapping);
        }
        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());
        }
        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);
        }
        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;
        }
Example #25
0
        // internal for testing
        internal static FunctionParameter[] CreateFunctionImportParameters(SimpleMappingContext mappingContext, EdmFunction storeFunction)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(storeFunction != null, "storeFunctionParameters != null");

            var functionImportParameters = new FunctionParameter[storeFunction.Parameters.Count];

            var uniqueParameterNames = new UniqueIdentifierService();

            for (var idx = 0; idx < storeFunction.Parameters.Count; idx++)
            {
                Debug.Assert(storeFunction.Parameters[idx].Mode == ParameterMode.In, "Only In parameters are supported.");

                var parameterName = CreateModelName(storeFunction.Parameters[idx].Name, uniqueParameterNames);

                if (parameterName != storeFunction.Parameters[idx].Name)
                {
                    mappingContext.Errors.Add(
                        new EdmSchemaError(
                            string.Format(
                                CultureInfo.InvariantCulture,
                                Resources_VersioningFacade.UnableToGenerateFunctionImportParameterName,
                                storeFunction.Parameters[idx].Name,
                                storeFunction.Name),
                            (int)ModelBuilderErrorCode.UnableToGenerateFunctionImportParameterName,
                            EdmSchemaErrorSeverity.Warning));
                    return(null);
                }

                functionImportParameters[idx] =
                    FunctionParameter.Create(
                        parameterName,
                        storeFunction.Parameters[idx].TypeUsage.ModelTypeUsage.EdmType,
                        storeFunction.Parameters[idx].Mode);
            }

            return(functionImportParameters);
        }
Example #26
0
        // internal for testing
        internal static FunctionImportMappingComposable BuildComposableFunctionMapping(
            EdmFunction storeFunction, SimpleMappingContext mappingContext)
        {
            Debug.Assert(storeFunction != null, "storeFunction != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var functionImport = mappingContext[storeFunction];

            Debug.Assert(
                functionImport.ReturnParameter.TypeUsage.EdmType is CollectionType &&
                ((CollectionType)functionImport.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType is ComplexType,
                "Return type should be collection of complex types");

            var returnComplexType =
                (ComplexType)((CollectionType)functionImport.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType;

            var structuralTypeMapping =
                new Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> >(
                    returnComplexType, new List <ConditionPropertyMapping>(), new List <PropertyMapping>());

            foreach (
                var storeProperty in
                ((RowType)((CollectionType)storeFunction.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType).Properties)
            {
                structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(mappingContext[storeProperty], storeProperty));
            }

            return
                (new FunctionImportMappingComposable(
                     functionImport,
                     storeFunction,
                     new List <Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> > >
            {
                structuralTypeMapping
            }));
        }
Example #27
0
        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.AddPropertyMapping(
                    new ScalarPropertyMapping(modelKeyMember, storeFkTableMember));
            }

            return(endPropertyMapping);
        }
        public void Can_add_and_get_function_mapping()
        {
            var storeFunction = EdmFunction.Create("fs", "ns", DataSpace.SSpace, new EdmFunctionPayload(), null);
            var functionImport = EdmFunction.Create("fs", "ns", DataSpace.SSpace, new EdmFunctionPayload(), null);

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

            mappingContext.AddMapping(storeFunction, functionImport);
            Assert.Same(functionImport, mappingContext[storeFunction]);
            Assert.Same(storeFunction, mappingContext.MappedStoreFunctions().Single());
        }
        public void Removing_entity_set_mapping_removes_corresponding_entity_type()
        {
            var storeEntity = CreateEntityType("storeEntity");
            var modelEntity = CreateEntityType("modelEntity");
            var storeEntitySet = EntitySet.Create("storeEntitySet", null, null, null, storeEntity, null);
            var modelEntitySet = EntitySet.Create("modelEntitySet", null, null, null, modelEntity, null);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            mappingContext.AddMapping(storeEntity, modelEntity);
            mappingContext.AddMapping(storeEntitySet, modelEntitySet);

            Assert.Same(modelEntitySet, mappingContext.ConceptualEntitySets().Single());
            Assert.Same(modelEntity, mappingContext.ConceptualEntityTypes().Single());

            mappingContext.RemoveMapping(storeEntitySet);

            Assert.Empty(mappingContext.ConceptualEntitySets());
            Assert.Empty(mappingContext.ConceptualEntityTypes());
        }
        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);
        }
        public void Can_add_and_get_mapping_for_collapsed_entity_sets()
        {
            var storeEntity = CreateEntityType("storeEntity");
            var storeEntitySet = EntitySet.Create("storeEntitySet", null, null, null, storeEntity, null);
            var collapsibleAssociationSet = new CollapsibleEntityAssociationSets(storeEntitySet);

            var associationType = AssociationType.Create("modelAssociationType", "ns", false, DataSpace.CSpace, null, null, null, null);
            var associationSet = AssociationSet.Create("modelAssociationType", associationType, null, null, null);

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

            Assert.Same(associationSet, mappingContext[collapsibleAssociationSet]);
        }
        private static AssociationSetMapping BuildAssociationSetMapping(
            AssociationSet storeAssociationSet,
            EntityContainerMapping entityContainerMapping,
            SimpleMappingContext mappingContext)
        {
            Debug.Assert(storeAssociationSet != null, "storeAssociationSet != null");
            Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var modelAssociationSet = mappingContext[storeAssociationSet];
            if (modelAssociationSet.ElementType.IsForeignKey)
            {
                return null;
            }

            var foreignKeyTableEnd = GetAssociationSetEndForForeignKeyTable(storeAssociationSet);
            var associationSetMapping = new AssociationSetMapping(
                modelAssociationSet, foreignKeyTableEnd.EntitySet, entityContainerMapping);

            var count = storeAssociationSet.AssociationSetEnds.Count;
            if (count > 0)
            {
                associationSetMapping.SourceEndMapping = BuildEndPropertyMapping(
                    storeAssociationSet.AssociationSetEnds[0], mappingContext);
            }
            if (count > 1)
            {
                associationSetMapping.TargetEndMapping = BuildEndPropertyMapping(
                    storeAssociationSet.AssociationSetEnds[1], mappingContext);
            }

            var constraint = GetReferentialConstraint(storeAssociationSet);
            foreach (var foreignKeyColumn in constraint.ToProperties)
            {
                if (foreignKeyColumn.Nullable)
                {
                    associationSetMapping.AddCondition(
                        new IsNullConditionMapping(foreignKeyColumn, false));
                }
            }

            return associationSetMapping;
        }
            BuildEntitySetMappings(EntityContainerMapping entityContainerMapping, SimpleMappingContext mappingContext)
        {
            Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            foreach (var storeEntitySet in mappingContext.StoreEntitySets())
            {
                var entitySetMapping = new EntitySetMapping(mappingContext[storeEntitySet], entityContainerMapping);
                entitySetMapping.AddTypeMapping(BuildEntityTypeMapping(entitySetMapping, mappingContext, storeEntitySet));
                yield return entitySetMapping;
            }
        }
        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));
        }
        // internal for testing
        internal static FunctionImportMappingComposable BuildComposableFunctionMapping(
            EdmFunction storeFunction, SimpleMappingContext mappingContext)
        {
            Debug.Assert(storeFunction != null, "storeFunction != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var functionImport = mappingContext[storeFunction];
            Debug.Assert(
                functionImport.ReturnParameter.TypeUsage.EdmType is CollectionType &&
                ((CollectionType)functionImport.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType is ComplexType,
                "Return type should be collection of complex types");

            var returnComplexType =
                (ComplexType)((CollectionType)functionImport.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType;

            var structuralTypeMapping =
                new Tuple<StructuralType, List<ConditionPropertyMapping>, List<PropertyMapping>>(
                    returnComplexType, new List<ConditionPropertyMapping>(), new List<PropertyMapping>());

            foreach (
                var storeProperty in
                    ((RowType)((CollectionType)storeFunction.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType).Properties)
            {
                structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(mappingContext[storeProperty], storeProperty));
            }

            return
                new FunctionImportMappingComposable(
                    functionImport,
                    storeFunction,
                    new List<Tuple<StructuralType, List<ConditionPropertyMapping>, List<PropertyMapping>>>
                        {
                            structuralTypeMapping
                        });
        }
        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);
        }
     BuildPropertyMapping(EntityType storeEntityType, SimpleMappingContext mappingContext)
 {
     return storeEntityType
         .Properties
         .Where(
             storeProperty =>
             mappingContext.IncludeForeignKeyProperties ||
             !mappingContext.StoreForeignKeyProperties.Contains(storeProperty) ||
             storeEntityType.KeyMembers.Contains(storeProperty))
         .Select(
             storeProperty => new ColumnMappingBuilder(
                                  storeProperty,
                                  new List<EdmProperty>
                                      {
                                          mappingContext[storeProperty]
                                      }));
 }
        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.AddPropertyMapping(
                    new ScalarPropertyMapping(modelKeyMember, storeFkTableMember));
            }

            return endPropertyMapping;
        }
        private static AssociationSetMapping BuildAssociationSetMapping(
            CollapsibleEntityAssociationSets collapsibleItem,
            EntityContainerMapping entityContainerMapping,
            SimpleMappingContext mappingContext)
        {
            Debug.Assert(collapsibleItem != null, "collapsibleItem != null");
            Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var modelAssociationSet = mappingContext[collapsibleItem];
            if (modelAssociationSet.ElementType.IsForeignKey)
            {
                return null;
            }

            var associationSetMapping = new AssociationSetMapping(
                modelAssociationSet, collapsibleItem.EntitySet, entityContainerMapping);

            var count = collapsibleItem.AssociationSets.Count;
            if (count > 0)
            {
                associationSetMapping.SourceEndMapping = BuildEndPropertyMapping(
                    collapsibleItem.GetStoreAssociationSetEnd(0).AssociationSetEnd, mappingContext);
            }
            if (count > 1)
            {
                associationSetMapping.TargetEndMapping = BuildEndPropertyMapping(
                    collapsibleItem.GetStoreAssociationSetEnd(1).AssociationSetEnd, mappingContext);
            }

            return associationSetMapping;
        }
        public void Can_add_get_association_set_mapping()
        {
            var storeAssociationSet =
                AssociationSet.Create(
                    "storeAssociationSet",
                    AssociationType.Create("storeAssociationType", "ns.Store", false, DataSpace.SSpace, null, null, null, null),
                    null, null, null);

            var conceptualAssociationSet =
                AssociationSet.Create(
                    "conceptualAssociationSet",
                    AssociationType.Create("conceptualAssociationType", "ns", false, DataSpace.CSpace, null, null, null, null),
                    null, null, null);

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

            Assert.Same(conceptualAssociationSet, mappingContext[storeAssociationSet]);
        }
Example #41
0
        private void GenerateAssociationSet(
            SimpleMappingContext mappingContext,
            CollapsibleEntityAssociationSets collapsibleItem,
            UniqueIdentifierService uniqueEntityContainerNames,
            UniqueIdentifierService globallyUniqueTypeNames)
        {
            var uniqueEndMemberNames = new UniqueIdentifierService(StringComparer.OrdinalIgnoreCase);

            var associationSetEndDetails0 = collapsibleItem.GetStoreAssociationSetEnd(0);

            var associationEndMember0 = GenerateAssociationEndMember(
                mappingContext,
                associationSetEndDetails0.AssociationSetEnd.CorrespondingAssociationEndMember,
                uniqueEndMemberNames,
                associationSetEndDetails0.Multiplicity,
                associationSetEndDetails0.DeleteBehavior);
            var conceptualEntitySet0 = mappingContext[associationSetEndDetails0.AssociationSetEnd.EntitySet];

            var associationSetEndDetails1 =
                collapsibleItem.GetStoreAssociationSetEnd(1);

            var associationEndMember1 = GenerateAssociationEndMember(
                mappingContext,
                associationSetEndDetails1.AssociationSetEnd.CorrespondingAssociationEndMember,
                uniqueEndMemberNames,
                associationSetEndDetails1.Multiplicity,
                associationSetEndDetails1.DeleteBehavior);
            var conceptualEntitySet1 = mappingContext[associationSetEndDetails1.AssociationSetEnd.EntitySet];

            globallyUniqueTypeNames.UnregisterIdentifier(mappingContext[collapsibleItem.EntitySet.ElementType].Name);
            uniqueEntityContainerNames.UnregisterIdentifier(mappingContext[collapsibleItem.EntitySet].Name);

            var associationTypeName = CreateModelName(collapsibleItem.EntitySet.Name, globallyUniqueTypeNames);
            var associationSetName  = CreateModelName(collapsibleItem.EntitySet.Name, uniqueEntityContainerNames);

            var conceptualAssociationType = AssociationType.Create(
                associationTypeName,
                _namespaceName,
                false,
                DataSpace.CSpace,
                associationEndMember0,
                associationEndMember1,
                null, // Don't need a referential constraint.
                null);

            CreateModelNavigationProperties(conceptualAssociationType);

            var conceptualAssociationSet = AssociationSet.Create(
                associationSetName,
                conceptualAssociationType,
                conceptualEntitySet0,
                conceptualEntitySet1,
                null);

            Debug.Assert(conceptualAssociationSet.AssociationSetEnds.Count == 2);
            var conceptualSetEnd0 = conceptualAssociationSet.AssociationSetEnds[0];
            var conceptualSetEnd1 = conceptualAssociationSet.AssociationSetEnds[1];

            mappingContext.AddMapping(associationSetEndDetails0.AssociationSetEnd, conceptualSetEnd0);
            mappingContext.AddMapping(associationSetEndDetails1.AssociationSetEnd, conceptualSetEnd1);
            mappingContext.AddMapping(collapsibleItem, conceptualAssociationSet);
            mappingContext.RemoveMapping(collapsibleItem.EntitySet);
        }
        public void BuildComposableFunctionMapping_creates_valid_function_mapping()
        {
            var rowTypeProperty = CreateStoreProperty("p1", "int");

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

            var functionImportReturnComplexType =
                ComplexType.Create(
                    "c",
                    "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 = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        functionImportReturnComplexType.GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                    null);

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

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

            var functionImportMapping =
                DbDatabaseMappingBuilder.BuildComposableFunctionMapping(storeFunction, mappingContext);

            Assert.NotNull(functionImportMapping);
            Assert.Same(storeFunction, functionImportMapping.TargetFunction);
            Assert.Same(functionImport, functionImportMapping.FunctionImport);

            var structuralTypeMappings = functionImportMapping.StructuralTypeMappings;

            Assert.NotNull(structuralTypeMappings);

            Assert.Same(functionImportReturnComplexType, structuralTypeMappings.Single().Item1);
            Assert.Empty(structuralTypeMappings.Single().Item2);
            Assert.Same(complexTypeProperty, structuralTypeMappings.Single().Item3.Single().Property);
            Assert.Same(rowTypeProperty, ((ScalarPropertyMapping)structuralTypeMappings.Single().Item3.Single()).Column);
        }
        public void Can_add_and_get_association_type_mapping()
        {
            var at1 = AssociationType.Create("at1", "ns", false, DataSpace.CSpace, null, null, null, null);
            var at2 = AssociationType.Create("at2", "ns", false, DataSpace.CSpace, null, null, null, null);

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

            Assert.Same(at2, mappingContext[at1]);
        }
        public void Can_add_get_association_type_mapping()
        {
            var storeAssociationType =
                AssociationType.Create("storeAssociationType", "ns.Store", false, DataSpace.SSpace, null, null, null, null);
            var conceptualAssociationType =
                AssociationType.Create("conceptualAssociationType", "ns", false, DataSpace.CSpace, null, null, null, null);

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

            Assert.Same(conceptualAssociationType, mappingContext[storeAssociationType]);

            AssociationType outAssociationType;
            Assert.True(mappingContext.TryGetValue(storeAssociationType, out outAssociationType));
            Assert.Same(conceptualAssociationType, outAssociationType);

            Assert.False(mappingContext.TryGetValue(conceptualAssociationType, out outAssociationType));
            Assert.Null(outAssociationType);
        }
        public void ConceptualAssociationSets_returns_associationsets_for_collapsed_entity_sets()
        {
            var storeEntity = CreateEntityType("storeEntity");
            var storeEntitySet = EntitySet.Create("storeEntitySet", null, null, null, storeEntity, null);
            var collapsibleAssociationSet = new CollapsibleEntityAssociationSets(storeEntitySet);

            var associationType = AssociationType.Create("modelAssociationType", "ns", false, DataSpace.CSpace, null, null, null, null);
            var associationSet = AssociationSet.Create("modelAssociationType", associationType, null, null, null);

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

            mappingContext.AddMapping(collapsibleAssociationSet, associationSet);
            Assert.Equal(1, mappingContext.ConceptualAssociationSets().Count());
            Assert.Same(associationSet, mappingContext.ConceptualAssociationSets().Single());
        }
            BuildEntityTypeMapping(EntitySetMapping storeEntitySetMapping, SimpleMappingContext mappingContext, EntitySet storeEntitySet)
        {
            Debug.Assert(storeEntitySetMapping != null, "storeEntitySetMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var entityType = storeEntitySetMapping.EntitySet.ElementType;

            var entityTypeMapping = new EntityTypeMapping(storeEntitySetMapping);
            entityTypeMapping.AddType(entityType);

            var mappingFragment = new MappingFragment(storeEntitySet, entityTypeMapping, false);
            entityTypeMapping.AddFragment(mappingFragment);

            foreach (var propertyMapping in BuildPropertyMapping(storeEntitySet.ElementType, mappingContext))
            {
                mappingFragment.AddColumnMapping(propertyMapping);
            }

            return entityTypeMapping;
        }
            BuildAssociationSetMappings(
            EntityContainerMapping entityContainerMapping,
            SimpleMappingContext mappingContext)
        {
            Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            var associationSetMappings = new List<AssociationSetMapping>();

            foreach (var associationSet in mappingContext.StoreAssociationSets())
            {
                var mapping = BuildAssociationSetMapping(associationSet, entityContainerMapping, mappingContext);
                if (mapping != null)
                {
                    associationSetMappings.Add(mapping);
                }
            }

            foreach (var collapsibleItem in mappingContext.CollapsedAssociationSets())
            {
                var mapping = BuildAssociationSetMapping(collapsibleItem, entityContainerMapping, mappingContext);
                if (mapping != null)
                {
                    associationSetMappings.Add(mapping);
                }
            }

            return associationSetMappings;
        }