Example #1
0
        protected virtual Dictionary <string, TphData> GetTphData()
        {
            var entitySetMaps = (IEnumerable <object>)MetadataWorkspace
                                .GetItemCollection(DataSpace.CSSpace)[0]
                                .GetPrivateFieldValue("EntitySetMaps");

            var data = new Dictionary <string, TphData>();

            foreach (var entitySetMap in entitySetMaps)
            {
                var props          = new List <EdmMember>();
                var navProps       = new List <NavigationProperty>();
                var discriminators = new Dictionary <string, object>();

                var typeMappings = (IEnumerable <object>)entitySetMap.GetPrivateFieldValue("TypeMappings");

                // make sure that the baseType appear last in the list
                typeMappings = typeMappings
                               .OrderBy(tm => ((IEnumerable <object>)tm.GetPrivateFieldValue("IsOfTypes")).Count());

                foreach (var typeMapping in typeMappings)
                {
                    var types            = (IEnumerable <EdmType>)typeMapping.GetPrivateFieldValue("Types");
                    var isOfypes         = (IEnumerable <EdmType>)typeMapping.GetPrivateFieldValue("IsOfTypes");
                    var mappingFragments = (IEnumerable <object>)typeMapping.GetPrivateFieldValue("MappingFragments");

                    // if isOfType.length > 0, then it is base type of TPH
                    // must merge properties with siblings
                    foreach (EntityType type in isOfypes)
                    {
                        var identity = type.ToString();
                        if (!data.ContainsKey(identity))
                        {
                            data[identity] = new TphData();
                        }

                        data[identity].NavProperties  = navProps.ToArray();
                        data[identity].Properties     = props.ToArray();
                        data[identity].Discriminators = discriminators;
                    }

                    foreach (EntityType type in types)
                    {
                        var identity = type.ToString();
                        if (!data.ContainsKey(identity))
                        {
                            data[identity] = new TphData();
                        }

                        // type.Properties gets properties including inherited properties
                        var tmp = new List <EdmMember>(type.Properties);

                        foreach (var navProp in type.NavigationProperties)
                        {
                            var associationType = navProp.RelationshipType as AssociationType;
                            if (associationType != null)
                            {
                                // if entity does not contain id property i.e has only reference object for a fk
                                if (associationType.ReferentialConstraints.Count == 0)
                                {
                                    tmp.Add(navProp);
                                }
                            }
                        }

                        data[identity].NavProperties = type.NavigationProperties.ToArray();
                        data[identity].Properties    = tmp.ToArray();

                        foreach (var prop in type.Properties)
                        {
                            if (!props.Contains(prop))
                            {
                                props.Add(prop);
                            }
                        }

                        foreach (var navProp in type.NavigationProperties)
                        {
                            if (!navProps.Contains(navProp))
                            {
                                navProps.Add(navProp);
                            }
                        }

                        foreach (var fragment in mappingFragments)
                        {
                            var conditionProperties = (IEnumerable)fragment.GetPrivateFieldValue("m_conditionProperties");
                            foreach (var conditionalProperty in conditionProperties)
                            {
                                var columnName = ((EdmProperty)conditionalProperty.GetPrivateFieldValue("Key")).Name;
                                var value      = conditionalProperty.GetPrivateFieldValue("Value").GetPrivateFieldValue("Value");

                                data[identity].Discriminators[columnName] = value;
                                discriminators[columnName] = value;
                            }
                        }
                    }
                }
            }

            return(data);
        }
Example #2
0
        protected virtual Dictionary<string, TphData> GetTphData()
        {
            var entitySetMaps = (IEnumerable<object>)MetadataWorkspace
                    .GetItemCollection(DataSpace.CSSpace)[0]
                    .GetPrivateFieldValue("EntitySetMaps");

            var data = new Dictionary<string, TphData>();

            foreach (var entitySetMap in entitySetMaps)
            {
                var props = new List<EdmMember>();
                var navProps = new List<NavigationProperty>();
                var discriminators = new Dictionary<string, object>();

                var typeMappings = (IEnumerable<object>)entitySetMap.GetPrivateFieldValue("TypeMappings");

                // make sure that the baseType appear last in the list
                typeMappings = typeMappings
                    .OrderBy(tm => ((IEnumerable<object>)tm.GetPrivateFieldValue("IsOfTypes")).Count());

                foreach (var typeMapping in typeMappings)
                {
                    var types = (IEnumerable<EdmType>)typeMapping.GetPrivateFieldValue("Types");
                    var isOfypes = (IEnumerable<EdmType>)typeMapping.GetPrivateFieldValue("IsOfTypes");
                    var mappingFragments = (IEnumerable<object>)typeMapping.GetPrivateFieldValue("MappingFragments");

                    // if isOfType.length > 0, then it is base type of TPH
                    // must merge properties with siblings
                    foreach (EntityType type in isOfypes)
                    {
                        var identity = type.ToString();
                        if (!data.ContainsKey(identity))
                            data[identity] = new TphData();

                        data[identity].NavProperties = navProps.ToArray();
                        data[identity].Properties = props.ToArray();
                        data[identity].Discriminators = discriminators;
                    }

                    foreach (EntityType type in types)
                    {
                        var identity = type.ToString();
                        if (!data.ContainsKey(identity))
                            data[identity] = new TphData();

                        // type.Properties gets properties including inherited properties
                        var tmp = new List<EdmMember>(type.Properties);

                        foreach (var navProp in type.NavigationProperties)
                        {
                            var associationType = navProp.RelationshipType as AssociationType;
                            if (associationType != null)
                            {
                                // if entity does not contain id property i.e has only reference object for a fk
                                if (associationType.ReferentialConstraints.Count == 0)
                                {
                                    tmp.Add(navProp);
                                }
                            }
                        }

                        data[identity].NavProperties = type.NavigationProperties.ToArray();
                        data[identity].Properties = tmp.ToArray();

                        foreach (var prop in type.Properties)
                        {
                            if (!props.Contains(prop))
                                props.Add(prop);
                        }

                        foreach (var navProp in type.NavigationProperties)
                        {
                            if (!navProps.Contains(navProp))
                                navProps.Add(navProp);
                        }

                        foreach (var fragment in mappingFragments)
                        {
                            var conditionProperties = (IEnumerable)fragment.GetPrivateFieldValue("m_conditionProperties");
                            foreach (var conditionalProperty in conditionProperties)
                            {
                                var columnName = ((EdmProperty)conditionalProperty.GetPrivateFieldValue("Key")).Name;
                                var value = conditionalProperty.GetPrivateFieldValue("Value").GetPrivateFieldValue("Value");

                                data[identity].Discriminators[columnName] = value;
                                discriminators[columnName] = value;
                            }
                        }
                    }
                }
            }

            return data;
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="typeFullName"></param>
        /// <param name="edmItem"></param>
        public EntityMap MapEntity(string typeFullName, EdmType edmItem)
        {
            var identity = edmItem.FullName;

            if (!TphData.ContainsKey(identity))
            {
                return(null);
            }

            var prepareMappingRes = PrepareMapping(typeFullName, edmItem);

            if (prepareMappingRes == null)
            {
                return(null);
            }

            var storageEntitySet = prepareMappingRes.StorageEntitySet;
            var tableName        = prepareMappingRes.TableName;
            var isRoot           = prepareMappingRes.IsRoot;
            var baseEdmType      = prepareMappingRes.BaseEdmType;

            var entityMap = RegEntity(typeFullName, tableName, storageEntitySet.Schema);

            entityMap.IsRoot        = isRoot;
            entityMap.EdmType       = edmItem;
            entityMap.ParentEdmType = entityMap.IsRoot ? null : baseEdmType;

            _pks[tableName] = storageEntitySet.ElementType.KeyMembers.Select(x => x.Name).ToArray();

            entityMap.IsTph = TphData[identity].Discriminators.Count > 0;

            string prefix = null;
            int    i      = 0;

            var propertiesToMap = this.GetPropertiesToMapOld(entityMap, storageEntitySet.ElementType.Properties);

            foreach (var edmProperty in propertiesToMap)
            {
                MapProperty(entityMap, edmProperty, ref i, ref prefix);
            }

            // create navigation properties
            foreach (var navigationProperty in TphData[identity].NavProperties)
            {
                var associationType = navigationProperty.RelationshipType as AssociationType;
                if (associationType == null || associationType.ReferentialConstraints.Count == 0)
                {
                    continue;
                }

                var to = associationType.ReferentialConstraints[0].ToProperties[0];

                var propertyMap = entityMap.Properties.Cast <PropertyMap>()
                                  .FirstOrDefault(x => x.EdmMember == to);

                if (propertyMap != null)
                {
                    propertyMap.NavigationPropertyName = navigationProperty.Name;

                    if (entityMap.Prop(navigationProperty.Name) == null)
                    {
                        var navigationPropertyMap = entityMap.MapProperty(navigationProperty.Name, propertyMap.ColumnName);
                        navigationPropertyMap.IsNavigationProperty   = true;
                        navigationPropertyMap.ForeignKeyPropertyName = propertyMap.PropertyName;
                        navigationPropertyMap.ForeignKey             = propertyMap;

                        propertyMap.NavigationProperty = navigationPropertyMap;
                    }
                }
            }

            // create discriminators
            foreach (var discriminator in TphData[identity].Discriminators)
            {
                var propertyMap = entityMap.MapDiscriminator(discriminator.Key, discriminator.Value);
                entityMap.AddDiscriminator(propertyMap);
            }

            return(entityMap);
        }