/// <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 temp = PrepareMapping(typeFullName, edmItem);

            if (temp == null)
            {
                return(null);
            }
            var storageEntitySet = temp.StorageEntitySet;
            var tableName        = temp.TableName;
            var isRoot           = temp.IsRoot;
            var baseEdmType      = temp.BaseEdmType;

            string schema = (string)storageEntitySet.MetadataProperties["Schema"].Value;

            var entityMap = RegEntity(typeFullName, tableName, 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;

            int i = 0;

            var propertiesToMap = GetPropertiesToMap(entityMap, storageEntitySet.ElementType.Properties).ToList();
            var fullName        = entityMap.EdmType.FullName;
            var entityMembers   = TphData[fullName].Properties;

            string prefix = null;

            foreach (var edmProperty in propertiesToMap)
            {
                if (entityMembers.Length <= i)
                {
                    break;
                }

                var columnName = edmProperty.Name;

                var edmMember2 = entityMembers.FirstOrDefault(e => {
                    var metadata      = edmProperty.MetadataProperties;
                    var preferredName = metadata.Any(m => m.Name == "PreferredName")
                        ? (string)metadata["PreferredName"].Value
                        : (string)metadata["Name"].Value;

                    var edmName    = e.Name;
                    var entityType = e.TypeUsage.EdmType.GetType();

                    if (entityType == typeof(ComplexType) || entityType == typeof(EntityType))
                    {
                        return(preferredName.StartsWith(edmName + "_"));
                    }

                    var ret = preferredName == edmName;
                    return(ret);
                });

                if (edmMember2 == null)
                {
                    i++;
                    continue;
                }

                // check if is complex type
                if (edmMember2.TypeUsage.EdmType.GetType() == typeof(ComplexType))
                {
                    prefix = edmMember2.Name;
                }

                string propName;
                if (prefix != null && columnName.StartsWith(prefix + "_"))
                {
                    propName = columnName.Replace('_', '.');
                }
                else
                {
                    propName = edmMember2.Name;
                    ++i;
                }

                var propInfo = entityMap.Type.GetProperty(propName, '.');
                if (propInfo == null)
                {
                    // entity does not contain such property
                    continue;
                }

                ActuallyAddAProperty(entityMap, edmProperty, propName, columnName, edmMember2);
            }

            // 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);
        }
        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);
        }
        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;
        }