public void Configure_should_update_table_name_when_base_type_is_null()
        {
            var entityMappingConfiguration
                = new EntityMappingConfiguration
                {
                TableName = new DatabaseName("Foo")
                };

            var entityTypeMapping = new EntityTypeMapping(null);

            entityTypeMapping.AddType(new EntityType("E", "N", DataSpace.CSpace));

            var databaseMapping =
                new DbDatabaseMapping().Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));

            var table     = databaseMapping.Database.AddTable("foo");
            var entitySet = databaseMapping.Database.GetEntitySet(table);

            entityTypeMapping.AddFragment(new MappingFragment(entitySet, entityTypeMapping, false));

            entityMappingConfiguration.Configure(
                databaseMapping, ProviderRegistry.Sql2008_ProviderManifest, entityTypeMapping.EntityType,
                ref entityTypeMapping, false, 0, 1,
                new Dictionary <string, object>());

            Assert.Equal("Foo", table.GetTableName().Name);
        }
コード例 #2
0
        private static bool RemapsInheritedProperties(
            DbDatabaseMapping databaseMapping, EntityTypeMapping entityTypeMapping)
        {
            var inheritedProperties = entityTypeMapping.EntityType.Properties
                                      .Except(entityTypeMapping.EntityType.DeclaredProperties)
                                      .Except(entityTypeMapping.EntityType.GetKeyProperties());

            foreach (var property in inheritedProperties)
            {
                var fragment = GetFragmentForPropertyMapping(entityTypeMapping, property);

                if (fragment != null)
                {
                    // find if this inherited property is mapped to another table by a base type
                    var baseType = (EntityType)entityTypeMapping.EntityType.BaseType;
                    while (baseType != null)
                    {
                        if (databaseMapping.GetEntityTypeMappings(baseType)
                            .Select(baseTypeMapping => GetFragmentForPropertyMapping(baseTypeMapping, property))
                            .Any(
                                baseFragment => baseFragment != null &&
                                baseFragment.Table != fragment.Table))
                        {
                            return(true);
                        }
                        baseType = (EntityType)baseType.BaseType;
                    }
                }
            }
            return(false);
        }
        internal void ConfigureTablesAndConditions(
            EntityTypeMapping entityTypeMapping,
            DbDatabaseMapping databaseMapping,
            DbProviderManifest providerManifest)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(providerManifest);

            var entityType
                = (entityTypeMapping != null)
                      ? entityTypeMapping.EntityType
                      : databaseMapping.Model.GetEntityType(ClrType);

            if (_entityMappingConfigurations.Any())
            {
                for (var i = 0; i < _entityMappingConfigurations.Count; i++)
                {
                    _entityMappingConfigurations[i]
                    .Configure(
                        databaseMapping,
                        providerManifest,
                        entityType,
                        ref entityTypeMapping,
                        IsMappingAnyInheritedProperty(entityType),
                        i,
                        _entityMappingConfigurations.Count,
                        _annotations);
                }
            }
            else
            {
                ConfigureUnconfiguredType(databaseMapping, providerManifest, entityType, _annotations);
            }
        }
コード例 #4
0
 private void ExtractCells(List <Cell> cells)
 {
     foreach (EntitySetBaseMapping allSetMap in this.m_containerMapping.AllSetMaps)
     {
         foreach (TypeMapping typeMapping in allSetMap.TypeMappings)
         {
             EntityTypeMapping entityTypeMapping = typeMapping as EntityTypeMapping;
             Set <EdmType>     allTypes          = new Set <EdmType>();
             if (entityTypeMapping != null)
             {
                 allTypes.AddRange((IEnumerable <EdmType>)entityTypeMapping.Types);
                 foreach (EdmType isOfType in entityTypeMapping.IsOfTypes)
                 {
                     IEnumerable <EdmType> typeAndSubtypesOf = MetadataHelper.GetTypeAndSubtypesOf(isOfType, (ItemCollection)this.m_containerMapping.StorageMappingItemCollection.EdmItemCollection, false);
                     allTypes.AddRange(typeAndSubtypesOf);
                 }
             }
             EntitySetBase set = allSetMap.Set;
             foreach (MappingFragment mappingFragment in typeMapping.MappingFragments)
             {
                 this.ExtractCellsFromTableFragment(set, mappingFragment, allTypes, cells);
             }
         }
     }
 }
コード例 #5
0
        private void GenerateIndependentAssociationType(
            AssociationType associationType,
            DbDatabaseMapping databaseMapping)
        {
            AssociationEndMember principalEnd;
            AssociationEndMember dependentEnd;

            if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                if (!associationType.IsPrincipalConfigured())
                {
                    throw Error.UnableToDeterminePrincipal((object)EntityTypeExtensions.GetClrType(associationType.SourceEnd.GetEntityType()), (object)EntityTypeExtensions.GetClrType(associationType.TargetEnd.GetEntityType()));
                }
                principalEnd = associationType.SourceEnd;
                dependentEnd = associationType.TargetEnd;
            }
            EntityTypeMapping     mappingInHierarchy = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType());
            EntityType            table = mappingInHierarchy.MappingFragments.First <MappingFragment>().Table;
            AssociationSetMapping associationSetMapping = AssociationTypeMappingGenerator.GenerateAssociationSetMapping(associationType, databaseMapping, principalEnd, dependentEnd, table);

            this.GenerateIndependentForeignKeyConstraint(databaseMapping, principalEnd.GetEntityType(), dependentEnd.GetEntityType(), table, associationSetMapping, associationSetMapping.SourceEndMapping, associationType.Name, principalEnd, false);
            foreach (EdmProperty keyProperty in dependentEnd.GetEntityType().KeyProperties())
            {
                associationSetMapping.TargetEndMapping.AddPropertyMapping(new ScalarPropertyMapping(keyProperty, mappingInHierarchy.GetPropertyMapping(keyProperty).ColumnProperty));
            }
        }
コード例 #6
0
        public void WriteMappingFragment_should_write_store_entity_set_name()
        {
            var fixture = new Fixture();

            var entityType      = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet       = new EntitySet("ES", "S", null, null, entityType);
            var entityContainer = new EntityContainer("EC", DataSpace.SSpace);

            entityContainer.AddEntitySetBase(entitySet);

            var storageEntitySetMapping
                = new EntitySetMapping(
                      entitySet,
                      new EntityContainerMapping(entityContainer));

            TypeMapping typeMapping = new EntityTypeMapping(storageEntitySetMapping);

            var mappingFragment = new MappingFragment(entitySet, typeMapping, false);

            fixture.Writer.WriteMappingFragmentElement(mappingFragment);

            Assert.Equal(
                @"<MappingFragment StoreEntitySet=""ES"" />",
                fixture.ToString());
        }
コード例 #7
0
        private bool FindPropertyEntityTypeMapping(
            TableMapping tableMapping,
            EntitySet entitySet,
            EntityType entityType,
            bool requiresIsTypeOf,
            out EntityTypeMapping entityTypeMapping,
            out MappingFragment fragment)
        {
            entityTypeMapping = (EntityTypeMapping)null;
            fragment          = (MappingFragment)null;
            var data = this._databaseMapping.GetEntityTypeMappings(entityType).SelectMany((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(etm => (IEnumerable <MappingFragment>)etm.MappingFragments), (etm, tmf) => new
            {
                etm = etm,
                tmf = tmf
            }).Where(_param1 => _param1.tmf.Table == tableMapping.Table).Select(_param0 => new
            {
                TypeMapping = _param0.etm,
                Fragment    = _param0.tmf
            }).SingleOrDefault();

            if (data == null)
            {
                return(false);
            }
            entityTypeMapping = data.TypeMapping;
            fragment          = data.Fragment;
            if (requiresIsTypeOf || !entityType.Abstract)
            {
                return(true);
            }
            this.RemoveFragment(entitySet, data.TypeMapping, data.Fragment);
            return(false);
        }
コード例 #8
0
        private static List <EntityTypeMapping> FindAllTypeMappingsUsingTable(
            DbDatabaseMapping databaseMapping,
            EntityType toTable)
        {
            List <EntityTypeMapping>       entityTypeMappingList = new List <EntityTypeMapping>();
            IList <EntityContainerMapping> containerMappings     = databaseMapping.EntityContainerMappings;

            for (int index1 = 0; index1 < containerMappings.Count; ++index1)
            {
                List <EntitySetMapping> list = containerMappings[index1].EntitySetMappings.ToList <EntitySetMapping>();
                for (int index2 = 0; index2 < list.Count; ++index2)
                {
                    ReadOnlyCollection <EntityTypeMapping> entityTypeMappings = list[index2].EntityTypeMappings;
                    for (int index3 = 0; index3 < entityTypeMappings.Count; ++index3)
                    {
                        EntityTypeMapping       entityTypeMapping = entityTypeMappings[index3];
                        EntityTypeConfiguration configuration     = entityTypeMapping.EntityType.GetConfiguration() as EntityTypeConfiguration;
                        for (int index4 = 0; index4 < entityTypeMapping.MappingFragments.Count; ++index4)
                        {
                            bool flag = configuration != null && configuration.IsTableNameConfigured;
                            if (!flag && entityTypeMapping.MappingFragments[index4].Table == toTable || flag && EntityMappingConfiguration.IsTableNameEqual(toTable, configuration.GetTableName()))
                            {
                                entityTypeMappingList.Add(entityTypeMapping);
                                break;
                            }
                        }
                    }
                }
            }
            return(entityTypeMappingList);
        }
コード例 #9
0
        public static void SetClrType(this EntityTypeMapping entityTypeMapping, Type type)
        {
            DebugCheck.NotNull(entityTypeMapping);
            DebugCheck.NotNull(type);

            entityTypeMapping.Annotations.SetClrType(type);
        }
コード例 #10
0
        private static EntityType FindParentTable(
            DbDatabaseMapping databaseMapping,
            EntityType fromTable,
            EntityTypeMapping entityTypeMapping,
            EntityType toTable,
            bool isMappingInheritedProperties,
            int configurationIndex,
            int configurationCount,
            out bool isSplitting)
        {
            EntityType entityType = (EntityType)null;

            isSplitting = false;
            if ((entityTypeMapping.UsesOtherTables(toTable) || configurationCount > 1) && configurationIndex != 0)
            {
                entityType  = entityTypeMapping.GetPrimaryTable();
                isSplitting = true;
            }
            if (entityType == null && fromTable != toTable && !isMappingInheritedProperties)
            {
                for (EdmType baseType = entityTypeMapping.EntityType.BaseType; baseType != null && entityType == null; baseType = baseType.BaseType)
                {
                    EntityTypeMapping entityTypeMapping1 = databaseMapping.GetEntityTypeMappings((EntityType)baseType).FirstOrDefault <EntityTypeMapping>();
                    if (entityTypeMapping1 != null)
                    {
                        entityType = entityTypeMapping1.GetPrimaryTable();
                    }
                }
            }
            return(entityType);
        }
コード例 #11
0
 private static bool RemapsInheritedProperties(
     DbDatabaseMapping databaseMapping,
     EntityTypeMapping entityTypeMapping)
 {
     foreach (EdmProperty edmProperty in entityTypeMapping.EntityType.Properties.Except <EdmProperty>((IEnumerable <EdmProperty>)entityTypeMapping.EntityType.DeclaredProperties).Except <EdmProperty>((IEnumerable <EdmProperty>)entityTypeMapping.EntityType.GetKeyProperties()))
     {
         EdmProperty     property = edmProperty;
         MappingFragment fragment = MappingInheritedPropertiesSupportConvention.GetFragmentForPropertyMapping(entityTypeMapping, property);
         if (fragment != null)
         {
             for (EntityType baseType = (EntityType)entityTypeMapping.EntityType.BaseType; baseType != null; baseType = (EntityType)baseType.BaseType)
             {
                 if (databaseMapping.GetEntityTypeMappings(baseType).Select <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, MappingFragment>)(baseTypeMapping => MappingInheritedPropertiesSupportConvention.GetFragmentForPropertyMapping(baseTypeMapping, property))).Any <MappingFragment>((Func <MappingFragment, bool>)(baseFragment =>
                 {
                     if (baseFragment != null)
                     {
                         return(baseFragment.Table != fragment.Table);
                     }
                     return(false);
                 })))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
コード例 #12
0
        private void RemoveFragment(
            EntitySet entitySet,
            EntityTypeMapping entityTypeMapping,
            MappingFragment fragment)
        {
            EdmProperty defaultDiscriminator = fragment.GetDefaultDiscriminator();

            if (defaultDiscriminator != null && entityTypeMapping.EntityType.BaseType != null && !entityTypeMapping.EntityType.Abstract)
            {
                ColumnMapping columnMapping = this._tableMappings[fragment.Table].ColumnMappings.SingleOrDefault <ColumnMapping>((Func <ColumnMapping, bool>)(cm => cm.Column == defaultDiscriminator));
                if (columnMapping != null)
                {
                    PropertyMappingSpecification mappingSpecification = columnMapping.PropertyMappings.SingleOrDefault <PropertyMappingSpecification>((Func <PropertyMappingSpecification, bool>)(pm => pm.EntityType == entityTypeMapping.EntityType));
                    if (mappingSpecification != null)
                    {
                        columnMapping.PropertyMappings.Remove(mappingSpecification);
                    }
                }
                defaultDiscriminator.Nullable = true;
            }
            if (entityTypeMapping.EntityType.Abstract)
            {
                foreach (ColumnMapping columnMapping in this._tableMappings[fragment.Table].ColumnMappings.Where <ColumnMapping>((Func <ColumnMapping, bool>)(cm => cm.PropertyMappings.All <PropertyMappingSpecification>((Func <PropertyMappingSpecification, bool>)(pm => pm.EntityType == entityTypeMapping.EntityType)))))
                {
                    fragment.Table.RemoveMember((EdmMember)columnMapping.Column);
                }
            }
            entityTypeMapping.RemoveFragment(fragment);
            if (entityTypeMapping.MappingFragments.Any <MappingFragment>())
            {
                return;
            }
            this._databaseMapping.GetEntitySetMapping(entitySet).RemoveTypeMapping(entityTypeMapping);
        }
コード例 #13
0
        private static MappingFragment FindConditionTypeMappingFragment(
            EntitySet tableSet, MappingFragment propertiesTypeMappingFragment,
            EntityTypeMapping conditionTypeMapping)
        {
            var table = tableSet.ElementType;

            var conditionTypeMappingFragment
                = conditionTypeMapping.MappingFragments
                  .SingleOrDefault(x => x.Table == table);

            if (conditionTypeMappingFragment == null)
            {
                conditionTypeMappingFragment
                    = EntityMappingOperations
                      .CreateTypeMappingFragment(conditionTypeMapping, propertiesTypeMappingFragment, tableSet);

                conditionTypeMappingFragment.SetIsConditionOnlyFragment(true);

                if (propertiesTypeMappingFragment.GetDefaultDiscriminator() != null)
                {
                    conditionTypeMappingFragment.SetDefaultDiscriminator(
                        propertiesTypeMappingFragment.GetDefaultDiscriminator());
                    propertiesTypeMappingFragment.RemoveDefaultDiscriminatorAnnotation();
                }
            }
            return(conditionTypeMappingFragment);
        }
        public void GetPropertyMapping_should_return_mapping_with_path()
        {
            var entityTypeMapping = new EntityTypeMapping(null);
            var propertyFoo       = EdmProperty.CreateComplex("Foo", new ComplexType("CT"));
            var propertyBar       = EdmProperty.CreatePrimitive("Bar", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            var entityPropertyMapping
                = new ColumnMappingBuilder(
                      new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })),
                      new[]
            {
                propertyFoo,
                propertyBar,
            });

            var entityTypeMappingFragment
                = new MappingFragment(new EntitySet(), entityTypeMapping, false);

            entityTypeMappingFragment.AddColumnMapping(entityPropertyMapping);
            entityTypeMapping.AddFragment(entityTypeMappingFragment);

            Assert.Same(entityPropertyMapping, entityTypeMapping.GetPropertyMapping(propertyFoo, propertyBar));
        }
コード例 #15
0
        public static void SetConfiguration(this EntityTypeMapping entityTypeMapping, object configuration)
        {
            DebugCheck.NotNull(entityTypeMapping);
            DebugCheck.NotNull(configuration);

            entityTypeMapping.Annotations.SetConfiguration(configuration);
        }
コード例 #16
0
        private void ProcessMappingFragments(EntityInfo info, EntityTypeMapping etm)
        {
            // process each relationship between the type and a table
            foreach (var table in info.Tables.Keys)
            {
                var tableInfo = info.Tables[table];

                // find or create the mapping fragment
                var frag = FindMappingFragment(etm, table);
                if (tableInfo.UsesEntityTypeMappingKind(etm.Kind))
                {
                    if (frag == null)
                    {
                        var cmd = new CreateMappingFragmentCommand(etm, table.EntitySet as StorageEntitySet);
                        CommandProcessor.InvokeSingleCommand(_cpc, cmd);
                        frag = cmd.MappingFragment;
                    }
                    Debug.Assert(frag != null, "Could not locate or create the required MappingFragment");

                    ProcessMappingFragment(info, table, frag);
                }
                else
                {
                    // don't need it, remove it if we have one
                    if (frag != null)
                    {
                        AddToDeleteList(frag);
                    }
                }
            }
        }
コード例 #17
0
        public static EntityTypeMapping Clone(this EntityTypeMapping entityTypeMapping)
        {
            EntityTypeMapping entityTypeMapping1 = new EntityTypeMapping((EntitySetMapping)null);

            entityTypeMapping1.AddType(entityTypeMapping.EntityType);
            entityTypeMapping.Annotations.Copy((ICollection <MetadataProperty>)entityTypeMapping1.Annotations);
            return(entityTypeMapping1);
        }
コード例 #18
0
        private static XElement CUDFunctionMapping(EntityType entityType, string entityContainerNamespace, string storeContainerNamespace)
        {
            EntityTypeMapping mapping = entityType.Mapping;

            if (mapping.InsertFunctionMapping == null || mapping.UpdateFunctionMapping == null || mapping.DeleteFunctionMapping == null)
            {
                return(null);
            }

            XElement modificationFunctionMapping = new XElement(mslNamespace + "ModificationFunctionMapping");

            var insertFunction = mapping.InsertFunctionMapping;

            if (insertFunction != null)
            {
                XElement insertFunctionElement = new XElement(mslNamespace + "InsertFunction",
                                                              new XAttribute("FunctionName", string.Concat(storeContainerNamespace, insertFunction.SSDLFunction.Name)));

                insertFunctionElement.Add(CUDFunctionMappingAssociation(insertFunction));
                insertFunctionElement.Add(CUDFunctionMappingParameters(insertFunction));
                insertFunctionElement.Add(CUDFunctionMappingResults(insertFunction));

                modificationFunctionMapping.AddElement(insertFunctionElement);
            }

            var updateFunction = mapping.UpdateFunctionMapping;

            if (updateFunction != null)
            {
                XElement updateFunctionElement = new XElement(mslNamespace + "UpdateFunction",
                                                              new XAttribute("FunctionName", string.Concat(storeContainerNamespace, updateFunction.SSDLFunction.Name)));

                updateFunctionElement.Add(CUDFunctionMappingAssociation(updateFunction));
                updateFunctionElement.Add(CUDFunctionMappingParameters(updateFunction));
                updateFunctionElement.Add(CUDFunctionMappingResults(updateFunction));

                modificationFunctionMapping.AddElement(updateFunctionElement);
            }

            var deleteFunction = mapping.DeleteFunctionMapping;

            if (deleteFunction != null)
            {
                XElement deleteFunctionElement = new XElement(mslNamespace + "DeleteFunction",
                                                              new XAttribute("FunctionName", string.Concat(storeContainerNamespace, deleteFunction.SSDLFunction.Name)));

                deleteFunctionElement.Add(CUDFunctionMappingAssociation(deleteFunction));
                deleteFunctionElement.Add(CUDFunctionMappingParameters(deleteFunction));
                deleteFunctionElement.Add(CUDFunctionMappingResults(deleteFunction));

                modificationFunctionMapping.AddElement(deleteFunctionElement);
            }

            return(new XElement(mslNamespace + "EntityTypeMapping",
                                new XAttribute("TypeName", string.Concat(entityContainerNamespace, entityType.Name)),
                                modificationFunctionMapping));
        }
コード例 #19
0
    public void Can_build_identity_model()
    {
        using (var context = CreateContext())
        {
            var entityTypeMappings = context.Model.GetEntityTypes().Select(e => new EntityTypeMapping(e)).ToList();

            EntityTypeMapping.AssertEqual(ExpectedMappings, entityTypeMappings);
        }
    }
コード例 #20
0
        /// <summary>
        ///     Creates a MappingFragment for the passed in StorageEntitySet in the passed in ETM.
        /// </summary>
        /// <param name="entityTypeMapping">This must a valid EntityTypeMapping.</param>
        /// <param name="entitySet">This must be a valid StorageEntitySet.</param>
        internal CreateMappingFragmentCommand(EntityTypeMapping entityTypeMapping, StorageEntitySet storageEntitySet)
        {
            CommandValidation.ValidateEntityTypeMapping(entityTypeMapping);
            CommandValidation.ValidateStorageEntitySet(storageEntitySet);

            ConceptualEntityType   = entityTypeMapping.FirstBoundConceptualEntityType;
            _entityTypeMappingKind = entityTypeMapping.Kind;
            _entityTypeMapping     = entityTypeMapping;
            StorageEntitySet       = storageEntitySet;
        }
コード例 #21
0
 private void WriteEntityTypeMappingElement(EntityTypeMapping entityTypeMapping)
 {
     this._xmlWriter.WriteStartElement("EntityTypeMapping");
     this._xmlWriter.WriteAttributeString("TypeName", MslXmlSchemaWriter.GetEntityTypeName(this._entityTypeNamespace + "." + entityTypeMapping.EntityType.Name, entityTypeMapping.IsHierarchyMapping));
     foreach (MappingFragment mappingFragment in entityTypeMapping.MappingFragments)
     {
         this.WriteMappingFragmentElement(mappingFragment);
     }
     this._xmlWriter.WriteEndElement();
 }
コード例 #22
0
        public static ColumnMappingBuilder GetPropertyMapping(
            this EntityTypeMapping entityTypeMapping, params EdmProperty[] propertyPath)
        {
            DebugCheck.NotNull(entityTypeMapping);
            DebugCheck.NotNull(propertyPath);
            Debug.Assert(propertyPath.Length > 0);

            return(entityTypeMapping.MappingFragments
                   .SelectMany(f => f.ColumnMappings)
                   .Single(p => p.PropertyPath.SequenceEqual(propertyPath)));
        }
コード例 #23
0
        /// <summary>
        ///     Create MappingFragment for the passed in ConceptualEntityType and StoreEntityType, and inside the
        ///     ETM based on the passed in kind.
        /// </summary>
        /// <param name="conceptualEntityType">This must be a valid EntityType from the C-Model.</param>
        /// <param name="storeEntityType">This must be a valid EntityType from the S-Model.</param>
        /// <param name="kind">Specify whether this should be put in an IsTypeOf or Default ETM</param>
        internal CreateMappingFragmentCommand(EntityType conceptualEntityType, EntityType storeEntityType, EntityTypeMappingKind kind)
        {
            CommandValidation.ValidateStorageEntityType(storeEntityType);
            CommandValidation.ValidateConceptualEntityType(conceptualEntityType);

            ConceptualEntityType   = conceptualEntityType;
            _entityTypeMappingKind = kind;
            _entityTypeMapping     = null;
            StorageEntitySet       = storeEntityType.EntitySet as StorageEntitySet;

            CommandValidation.ValidateStorageEntitySet(StorageEntitySet);
        }
コード例 #24
0
        public static EntityTypeMapping Clone(this EntityTypeMapping entityTypeMapping)
        {
            DebugCheck.NotNull(entityTypeMapping);

            var clone = new EntityTypeMapping(null);

            clone.AddType(entityTypeMapping.EntityType);

            entityTypeMapping.Annotations.Copy(clone.Annotations);

            return(clone);
        }
コード例 #25
0
        internal static void ConfigureUnconfiguredType(
            DbDatabaseMapping databaseMapping,
            ICollection <EntitySet> entitySets,
            DbProviderManifest providerManifest,
            EntityType entityType,
            IDictionary <string, object> commonAnnotations)
        {
            EntityMappingConfiguration mappingConfiguration = new EntityMappingConfiguration();
            EntityTypeMapping          entityTypeMapping    = databaseMapping.GetEntityTypeMapping(EntityTypeExtensions.GetClrType(entityType));

            mappingConfiguration.Configure(databaseMapping, entitySets, providerManifest, entityType, ref entityTypeMapping, false, 0, 1, commonAnnotations);
        }
コード例 #26
0
        private static MappingFragment FindMappingFragment(EntityTypeMapping etm, EntityType table)
        {
            foreach (var fragment in etm.MappingFragments())
            {
                if (fragment.StoreEntitySet.Target == table.EntitySet)
                {
                    return(fragment);
                }
            }

            return(null);
        }
コード例 #27
0
 public DataLoader(string edmxFile, string dataFile)
 {
     Initialize(dataFile);
     using (EdmxReader <EntityTypeMapping> edmxReader = new EdmxReader <EntityTypeMapping>(edmxFile))
     {
         if (!edmxReader.TryLoad())
         {
             throw new Exception("Failed to load the edmx reader");
         }
         _EntityMapping = GetEntityTypeMapping(edmxReader);
     }
 }
コード例 #28
0
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // make sure that there isn't an ETM of this kind already
            var entityTypeMapping = ModelHelper.FindEntityTypeMapping(cpc, _entityType, _kind, false);

            Debug.Assert(entityTypeMapping == null, "We are calling CreateEntityTypeMappingCommand and there is already one of this Kind");
            if (entityTypeMapping != null)
            {
                _created = entityTypeMapping;
                return;
            }

            // see if we can get the EntitySetMapping for our entity (if we weren't passed it)
            if (_entitySetMapping == null)
            {
                var ces = _entityType.EntitySet as ConceptualEntitySet;
                Debug.Assert(ces != null, "_entityType.EntitySet should be a ConceptualEntitySet");

                // find the EntitySetMapping for this type (V1 assumption is that there is only ESM per ES)
                EntitySetMapping esm = null;
                foreach (var depMapping in ces.GetAntiDependenciesOfType <EntitySetMapping>())
                {
                    esm = depMapping;
                    break;
                }

                _entitySetMapping = esm;
            }

            // if we still don't have an ESM, create one
            if (_entitySetMapping == null)
            {
                var cmd = new CreateEntitySetMappingCommand(
                    _entityType.Artifact.MappingModel().FirstEntityContainerMapping,
                    _entityType.EntitySet as ConceptualEntitySet);
                CommandProcessor.InvokeSingleCommand(cpc, cmd);
                _entitySetMapping = cmd.EntitySetMapping;
            }
            Debug.Assert(
                _entitySetMapping != null,
                "_entitySetMapping should not be null - we have been unable to find or create an EntitySetMapping");

            // create the ETM
            var etm = new EntityTypeMapping(_entitySetMapping, null, _kind);

            etm.TypeName.SetRefName(_entityType);
            _entitySetMapping.AddEntityTypeMapping(etm);

            XmlModelHelper.NormalizeAndResolve(etm);

            _created = etm;
        }
コード例 #29
0
        private EntityTypeMapping GetEntityTypeMapping(EdmxReader <EntityTypeMapping> edmxReader)
        {
            EntityTypeMapping entityTypeMapping = edmxReader.Nodes.SingleOrDefault(
                e => e.MappingFragment.StoreEntitySet.ToLower().Equals(MappingName)
                );

            if (entityTypeMapping == null)
            {
                throw new IndexOutOfRangeException("The file specified is not apart of the mapping. File: " + DataFileName);
            }

            return(entityTypeMapping);
        }
コード例 #30
0
        private HashSet <EdmPropertyPath> DiscoverAllMappingsToContain(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            EntityType toTable,
            bool isSharingTableWithBase)
        {
            HashSet <EdmPropertyPath> mappingsToContain = new HashSet <EdmPropertyPath>();

            entityType.KeyProperties().Each <EdmProperty>((Action <EdmProperty>)(p => mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
            if (this.MapInheritedProperties)
            {
                entityType.Properties.Except <EdmProperty>((IEnumerable <EdmProperty>)entityType.DeclaredProperties).Each <EdmProperty>((Action <EdmProperty>)(p => mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
            }
            if (isSharingTableWithBase)
            {
                HashSet <EdmPropertyPath> baseMappingsToContain = new HashSet <EdmPropertyPath>();
                EntityType        baseType          = (EntityType)entityType.BaseType;
                EntityTypeMapping entityTypeMapping = (EntityTypeMapping)null;
                MappingFragment   mappingFragment   = (MappingFragment)null;
                for (; baseType != null && entityTypeMapping == null; baseType = (EntityType)baseType.BaseType)
                {
                    entityTypeMapping = databaseMapping.GetEntityTypeMapping((EntityType)entityType.BaseType);
                    if (entityTypeMapping != null)
                    {
                        mappingFragment = entityTypeMapping.MappingFragments.SingleOrDefault <MappingFragment>((Func <MappingFragment, bool>)(tmf => tmf.Table == toTable));
                    }
                    if (mappingFragment == null)
                    {
                        baseType.DeclaredProperties.Each <EdmProperty>((Action <EdmProperty>)(p => baseMappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
                    }
                }
                if (mappingFragment != null)
                {
                    foreach (ColumnMappingBuilder columnMapping in mappingFragment.ColumnMappings)
                    {
                        mappingsToContain.Add(new EdmPropertyPath((IEnumerable <EdmProperty>)columnMapping.PropertyPath));
                    }
                }
                mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)baseMappingsToContain);
            }
            if (this.Properties == null)
            {
                entityType.DeclaredProperties.Each <EdmProperty>((Action <EdmProperty>)(p => mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
            }
            else
            {
                this.Properties.Each <PropertyPath>((Action <PropertyPath>)(p => mappingsToContain.AddRange <EdmPropertyPath>(EntityMappingConfiguration.PropertyPathToEdmPropertyPath(p, entityType))));
            }
            return(mappingsToContain);
        }