private void CreateStorageEntityTypeProperties(ISchemaEntity entity, EntityTypeStore entityType)
        {
            //<Property Name="CategoryId" Type="varchar" Nullable="false" MaxLength="10"  />
            foreach (ISchemaProperty property in entity.Properties) {
                var entityProperty = entityType.Properties.Where(p => property.KeyName.Equals(p.Name, StringComparison.OrdinalIgnoreCase) || property.KeyName.Equals(p.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (entityProperty == null) {
                    if (ExcludeProperty(property))
                        continue;

                    entityProperty = new EntityProperty() {
                        Name = property.KeyName
                    };
                    entityType.Properties.Add(entityProperty);

                    _newStorageEntityProperties.Add(String.Format("{0}-{1}", entity.Name, property.Name));
                } else if (ExcludeProperty(property)) {
                    entityType.Properties.Remove(entityProperty);
                    continue;
                }

                entityProperty.Name = property.KeyName;
                entityProperty.Type = GetNativeType(property);

                if (!property.IsNullable)
                    entityProperty.Nullable = property.IsNullable;

                if (String.IsNullOrEmpty(entityProperty.DefaultValue) && !String.IsNullOrEmpty(property.DefaultValue)) {
                    if (property.DefaultValue.ToLowerInvariant().Contains("autoincrement")) // Needed for sql anywhere
                        entityProperty.DefaultValue = null;
                    else if (String.Equals(property.BaseSystemType, "System.Boolean", StringComparison.OrdinalIgnoreCase))
                        entityProperty.DefaultValue = property.DefaultValue.ToLower();
                    else if (String.Equals(property.BaseSystemType, "System.Single", StringComparison.OrdinalIgnoreCase)
                             || String.Equals(property.BaseSystemType, "System.Int16", StringComparison.OrdinalIgnoreCase)
                             || String.Equals(property.BaseSystemType, "System.Int32", StringComparison.OrdinalIgnoreCase)
                             || String.Equals(property.BaseSystemType, "System.Int64", StringComparison.OrdinalIgnoreCase)
                             || String.Equals(property.BaseSystemType, "System.Byte", StringComparison.OrdinalIgnoreCase)
                             || String.Equals(property.BaseSystemType, "System.Decimal", StringComparison.OrdinalIgnoreCase)
                             || String.Equals(property.BaseSystemType, "System.Double", StringComparison.OrdinalIgnoreCase)
                             || String.Equals(property.BaseSystemType, "System.String", StringComparison.OrdinalIgnoreCase))
                        entityProperty.DefaultValue = property.DefaultValue;
                    else
                        entityProperty.DefaultValue = null;
                } else {
                    entityProperty.DefaultValue = null;
                }

                entityProperty.MaxLength = !String.IsNullOrEmpty(GetMaxLength(property)) && !GetMaxLength(property).Equals("Max", StringComparison.OrdinalIgnoreCase) && !GetNativeType(property).Equals("timestamp", StringComparison.OrdinalIgnoreCase) ? GetMaxLength(property) : null;

                entityProperty.StoreGeneratedPattern = property.IsIdentity ? EdmxConstants.StoreGeneratedPatternIdentity : property.IsComputed ? EdmxConstants.StoreGeneratedPatternComputed : null;
            }
        }
        private static void RemoveDuplicateStorageEntityTypeKeysAndProperties(EntityTypeStore entity)
        {
            var processed = new List<string>();
            var propertiesToRemove = new List<EntityProperty>();
            foreach (var property in entity.Properties) {
                if (processed.Contains(property.Name))
                    propertiesToRemove.Add(property);
                else
                    processed.Add(property.Name);
            }

            foreach (var e in propertiesToRemove) {
                entity.Properties.Remove(e);
            }

            entity.Properties = (from p in entity.Properties select p).ToList();

            if (entity.Key != null) {
                var keysProcessed = new List<string>();
                var keysToRemove = new List<PropertyRef>();
                foreach (var property in entity.Key.PropertyRefs) {
                    if (!processed.Contains(property.Name) || keysProcessed.Contains(property.Name))
                        keysToRemove.Add(property);
                    else
                        keysProcessed.Add(property.Name);
                }

                foreach (var e in keysToRemove) {
                    entity.Key.PropertyRefs.Remove(e);
                }
            }
        }
        private EntityTypeStore CreateStorageEntityType(ISchemaEntity entity, string name, ref bool isNewView)
        {
            EntityTypeStore entityType = StorageSchema.EntityTypeStores.Where(e => ResolveStorageEntityName(entity.EntityKeyName).Equals(e.Name, StringComparison.OrdinalIgnoreCase) || name.Equals(e.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            if (entityType == null) {
                entityType = new EntityTypeStore() {
                    Name = name,
                    Key = new EntityKeyElement()
                };

                StorageSchema.EntityTypeStores.Add(entityType);

                isNewView = entity is ViewEntity;
            }

            // Sync the name.
            entityType.Name = name;

            return entityType;
        }
        private static void CreateStorageEntityTypeKeys(ISchemaEntity entity, bool isNewView, EntityTypeStore entityType)
        {
            //<Key>
            //  <PropertyRef Name="CategoryId"  />
            //</Key>
            if (entity.HasKey || isNewView) {
                #region Remove extra key values.

                var items = from property in entityType.Key.PropertyRefs
                    where !(from prop in entity.Key.Properties select prop.KeyName).Contains(property.Name)
                    select property;

                // Remove all of the key properties that don't exist in the table entity.
                foreach (var property in items) {
                    entityType.Key.PropertyRefs.Remove(property);
                }

                #endregion

                foreach (var property in entity.Key.Properties.Where(p => entityType.Key.PropertyRefs.Count(pr => pr.Name == p.Name) == 0)) {
                    entityType.Key.PropertyRefs.Add(new PropertyRef() {
                        Name = property.KeyName
                    });
                }
            } else if (entity is TableEntity) {
                entityType.Key.PropertyRefs.Clear();
            }
        }