Ejemplo n.º 1
0
        // internal for testing
        internal static string CreateFunctionImportEntityTypeMappingTypeName(FunctionImportEntityTypeMapping entityTypeMapping)
        {
            var entityTypeName =
                string.Join(
                    ";",
                    entityTypeMapping.EntityTypes.Select(e => GetEntityTypeName(e.FullName, false))
                    .Concat(entityTypeMapping.IsOfTypeEntityTypes.Select(e => GetEntityTypeName(e.FullName, true))));

            return(entityTypeName);
        }
 private void WriteFunctionImportEntityTypeMappingElement(
     FunctionImportEntityTypeMapping entityTypeMapping)
 {
     this._xmlWriter.WriteStartElement("EntityTypeMapping");
     this._xmlWriter.WriteAttributeString("TypeName", MslXmlSchemaWriter.CreateFunctionImportEntityTypeMappingTypeName(entityTypeMapping));
     this.WriteFunctionImportPropertyMappingElements(entityTypeMapping.PropertyMappings.Cast <FunctionImportReturnTypeScalarPropertyMapping>());
     foreach (FunctionImportEntityTypeMappingCondition condition in entityTypeMapping.Conditions)
     {
         this.WriteFunctionImportConditionElement(condition);
     }
     this._xmlWriter.WriteEndElement();
 }
 private void WriteFunctionImportResultMappingElement(FunctionImportResultMapping resultMapping)
 {
     this._xmlWriter.WriteStartElement("ResultMapping");
     foreach (FunctionImportStructuralTypeMapping typeMapping in resultMapping.TypeMappings)
     {
         FunctionImportEntityTypeMapping entityTypeMapping = typeMapping as FunctionImportEntityTypeMapping;
         if (entityTypeMapping != null)
         {
             this.WriteFunctionImportEntityTypeMappingElement(entityTypeMapping);
         }
         else
         {
             this.WriteFunctionImportComplexTypeMappingElement((FunctionImportComplexTypeMapping)typeMapping);
         }
     }
     this._xmlWriter.WriteEndElement();
 }
        private void MergeScalarProperties(FunctionImportEntityTypeMapping mappingFragment, CommandEntity entity)
        {
            foreach (var property in mappingFragment.ScalarProperties.Where(p => entity.Properties.Count(prop => prop.KeyName.Equals(p.ColumnName, StringComparison.OrdinalIgnoreCase)) == 0))
            {
                _mappingDroppedEntityPropertyNames[String.Format(PROPERTY_KEY, entity.EntityKeyName, property.ColumnName)] = property.Name;
            }

            var properties = new List <ScalarProperty>();

            foreach (var property in entity.Properties)
            {
                var prop = mappingFragment.ScalarProperties.Where(p => p.ColumnName.Equals(property.KeyName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (prop == null)
                {
                    // The property doesn't exist so lets create it.
                    prop = new ScalarProperty()
                    {
                        Name = property.Name
                    };
                }
                else if (!property.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase)) // Column matches that in the database.. If the names are different, it wins.
                {
                    // The propertyName has been updated.
                    // TODO: Is there a better way to find out if they renamed the Property?
                    //prop.Name = prop.Name;
                }
                else
                {
                    // Update the propertyName so it is always current with SchemaHelper.
                    prop.Name = property.Name;
                }

                prop.ColumnName = property.KeyName;
                if (!ExcludeProperty(property as ISchemaProperty) && properties.Count(p => p.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase)) == 0)
                {
                    properties.Add(prop);
                    _mappingEntityPropertyNames[String.Format("{0}-{1}", entity.Name, property.KeyName)] = prop.Name;
                }
            }

            mappingFragment.ScalarProperties = properties.Distinct().ToList();
        }
Ejemplo n.º 5
0
        private void WriteFunctionImportEntityTypeMappingElement(FunctionImportEntityTypeMapping entityTypeMapping)
        {
            DebugCheck.NotNull(entityTypeMapping);

            _xmlWriter.WriteStartElement(MslConstructs.EntityTypeMappingElement);

            var entityTypeName = CreateFunctionImportEntityTypeMappingTypeName(entityTypeMapping);

            _xmlWriter.WriteAttributeString(MslConstructs.EntityTypeMappingTypeNameAttribute, entityTypeName);

            WriteFunctionImportPropertyMappingElements(
                entityTypeMapping.PropertyMappings.Cast <FunctionImportReturnTypeScalarPropertyMapping>());

            foreach (var condition in entityTypeMapping.Conditions)
            {
                WriteFunctionImportConditionElement(condition);
            }

            _xmlWriter.WriteEndElement();
        }
        private void CreateFunctionMappingEntityTypeMapping(CommandEntity entity, FunctionImportMapping importMapping)
        {
            //<ResultMapping>
            //  <EntityTypeMapping TypeName="PetShopModel.GetCategoryById">
            string entityName = entity.AssociatedEntity.Name;
            var    mapping    = importMapping.ResultMapping != null && importMapping.ResultMapping.EntityTypeMappings != null
                              ? importMapping.ResultMapping.EntityTypeMappings.FirstOrDefault()
                              : null;

            if (mapping == null)
            {
                importMapping.ResultMapping = new FunctionImportMappingResultMapping()
                {
                    EntityTypeMappings = new List <FunctionImportEntityTypeMapping>()
                };

                mapping = new FunctionImportEntityTypeMapping()
                {
                    TypeName = String.Concat(ConceptualSchema.Namespace, ".", entityName)
                };
                importMapping.ResultMapping.EntityTypeMappings.Add(mapping);
            }
            else if (!String.IsNullOrEmpty(mapping.TypeName))
            {
                entityName = mapping.TypeName.Replace("IsTypeOf(", "").Replace(String.Format("{0}.", ConceptualSchema.Namespace), "").Replace(")", "");
                entityName = entityName.Equals(entity.Name, StringComparison.OrdinalIgnoreCase) ? entity.Name : entityName;
            }

            // Check for inheritance.
            mapping.TypeName = String.Format("{0}.{1}", ConceptualSchema.Namespace, entityName);

            _mappingEntityNames.Add(entity.EntityKey(), importMapping.FunctionImportName);

            //<EntityTypeMapping TypeName="PetShopModel.GetCategoryById">
            //  <ScalarProperty Name="CategoryId" ColumnName="CategoryId" />
            //  <ScalarProperty Name="Name" ColumnName="Name" />
            //  <ScalarProperty Name="Description" ColumnName="Descn" />
            //</EntityTypeMapping>
            MergeScalarProperties(mapping, entity);
        }
Ejemplo n.º 7
0
        public void CreateFunctionImportEntityTypeMappingTypeName_returns_correct_TypeName_attribute_value()
        {
            var ofEntityTypes = new[]
            {
                new EntityType("OfType1", "Ns", DataSpace.CSpace),
                new EntityType("OfType2", "Ns", DataSpace.CSpace)
            };

            var entityTypes = new[]
            {
                new EntityType("ET1", "Ns", DataSpace.CSpace),
                new EntityType("ET2", "Ns", DataSpace.CSpace)
            };

            var mapping = new FunctionImportEntityTypeMapping(
                ofEntityTypes,
                entityTypes,
                new Collection <FunctionImportReturnTypePropertyMapping>(),
                new List <FunctionImportEntityTypeMappingCondition>());

            Assert.Equal(
                "Ns.ET1;Ns.ET2;IsTypeOf(Ns.OfType1);IsTypeOf(Ns.OfType2)",
                MslXmlSchemaWriter.CreateFunctionImportEntityTypeMappingTypeName(mapping));
        }
 internal static string CreateFunctionImportEntityTypeMappingTypeName(
     FunctionImportEntityTypeMapping entityTypeMapping)
 {
     return(string.Join(";", entityTypeMapping.EntityTypes.Select <EntityType, string>((Func <EntityType, string>)(e => MslXmlSchemaWriter.GetEntityTypeName(e.FullName, false))).Concat <string>(entityTypeMapping.IsOfTypeEntityTypes.Select <EntityType, string>((Func <EntityType, string>)(e => MslXmlSchemaWriter.GetEntityTypeName(e.FullName, true))))));
 }
Ejemplo n.º 9
0
        public void Apply(EntityContainer item, DbModel model)
        {
            var functionDescriptors  = new FunctionDiscovery(model, _methodClassType).FindFunctions();
            var storeFunctionBuilder = new StoreFunctionBuilder(model, _defaultSchema);

            foreach (var functionDescriptor in functionDescriptors)
            {
                var storeFunctionDefinition = storeFunctionBuilder.Create(functionDescriptor);
                model.StoreModel.AddItem(storeFunctionDefinition);

                if (functionDescriptor.StoreFunctionKind != StoreFunctionKind.ScalarUserDefinedFunction)
                {
                    var functionImportDefinition = CreateFunctionImport(model, functionDescriptor);
                    model.ConceptualModel.Container.AddFunctionImport(functionImportDefinition);

                    List <FunctionImportResultMapping> resultMappings = new List <FunctionImportResultMapping>();
                    if (functionDescriptor.ReturnTypes.All(t => t is EntityType || t is ComplexType))
                    {
                        foreach (EdmType returnType in functionDescriptor.ReturnTypes)
                        {
                            FunctionImportStructuralTypeMapping typeMapping;
                            if (returnType is EntityType)
                            {
                                var entityType = (EntityType)returnType;

                                var returnTypePropertyMappings = new Collection <FunctionImportReturnTypePropertyMapping>();
                                foreach (var propertyMapping in model.GetEntityTypePropertyMappings(entityType).OfType <ScalarPropertyMapping>())
                                {
                                    returnTypePropertyMappings.Add(new FunctionImportReturnTypeScalarPropertyMapping(propertyMapping.Property.Name, propertyMapping.Column.Name));
                                }

                                typeMapping = new FunctionImportEntityTypeMapping(
                                    Enumerable.Empty <EntityType>(),
                                    new[] { entityType },
                                    returnTypePropertyMappings,
                                    Enumerable.Empty <FunctionImportEntityTypeMappingCondition>());
                            }
                            else // ComplexType
                            {
                                var complexType = (ComplexType)returnType;

                                var returnTypePropertyMappings = new Collection <FunctionImportReturnTypePropertyMapping>();
                                foreach (var property in complexType.Properties)
                                {
                                    returnTypePropertyMappings.Add(new FunctionImportReturnTypeScalarPropertyMapping(property.Name, property.Name));
                                }

                                typeMapping = new FunctionImportComplexTypeMapping(complexType, returnTypePropertyMappings);
                            }

                            FunctionImportResultMapping resultMapping = new FunctionImportResultMapping();
                            resultMappings.Add(resultMapping);
                            resultMapping.AddTypeMapping(typeMapping);
                        }
                    }

                    if (functionImportDefinition.IsComposableAttribute)
                    {
                        model.ConceptualToStoreMapping.AddFunctionImportMapping(
                            new FunctionImportMappingComposable(
                                functionImportDefinition,
                                storeFunctionDefinition,
                                resultMappings.FirstOrDefault() ?? new FunctionImportResultMapping(),
                                model.ConceptualToStoreMapping));
                    }
                    else
                    {
                        // HACK: Currently, FunctionImportMappingNonComposable ctor does not support code-first construction because
                        //       it depends on EdmItemCollection being available from StorageMappingItemCollection. Code-first does
                        //       not create a StorageMappingItemCollection and, as a result, this ctor will throw a null reference
                        //       exception if any result mappings are passed to it in code-first context. This must be resolved in
                        //       EF itself. Until then, _only composable functions can support custom named column mappings_. Once
                        //       this issue is resolved in EF then the commented code should replace the current "empty array"
                        //       resultMapping parameter to enable custom mappings for non-composable functions as well:
                        model.ConceptualToStoreMapping.AddFunctionImportMapping(
                            new FunctionImportMappingNonComposable(
                                functionImportDefinition,
                                storeFunctionDefinition,
                                // resultMappings.Any() ? resultMappings.ToArray() : new FunctionImportResultMapping[0],
                                new FunctionImportResultMapping[0],
                                model.ConceptualToStoreMapping));
                    }
                }
            }

            // TODO: model defined functions?
        }