private void RediscoverComplexTypes(IEnumerable <IStructuralTypeConfiguration> explicitlyAddedTypes) { IEnumerable <IEntityTypeConfiguration> misconfiguredEntityTypes = StructuralTypes .Except(explicitlyAddedTypes) .OfType <IEntityTypeConfiguration>() .Where(entity => !entity.Keys.Any()) .ToArray(); IEnumerable <IEntityTypeConfiguration> actualEntityTypes = StructuralTypes .Except(misconfiguredEntityTypes) .OfType <IEntityTypeConfiguration>() .ToArray(); foreach (IEntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes) { RemoveStructuralType(misconfiguredEntityType.ClrType); foreach (IEntityTypeConfiguration entityToBePatched in actualEntityTypes) { NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched .NavigationProperties .Where(navigationProperty => navigationProperty.RelatedClrType == misconfiguredEntityType.ClrType) .ToArray(); foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved) { entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo); entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo); } } AddComplexType(misconfiguredEntityType.ClrType); } }
private void RediscoverComplexTypes() { Contract.Assert(_explicitlyAddedTypes != null); EntityTypeConfiguration[] misconfiguredEntityTypes = StructuralTypes .Except(_explicitlyAddedTypes) .OfType <EntityTypeConfiguration>() .Where(entity => !entity.Keys().Any()) .ToArray(); ReconfigureEntityTypesAsComplexType(misconfiguredEntityTypes); }
private void ReconfigureEntityTypesAsComplexType(EntityTypeConfiguration[] misconfiguredEntityTypes) { IEnumerable <EntityTypeConfiguration> actualEntityTypes = StructuralTypes .Except(misconfiguredEntityTypes) .OfType <EntityTypeConfiguration>(); foreach (EntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes) { RemoveStructuralType(misconfiguredEntityType.ClrType); // this is a wrongly inferred type. so just ignore any pending configuration from it. AddComplexType(misconfiguredEntityType.ClrType); foreach (EntityTypeConfiguration entityToBePatched in actualEntityTypes) { NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched .NavigationProperties .Where(navigationProperty => navigationProperty.RelatedClrType == misconfiguredEntityType.ClrType) .ToArray(); foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved) { string propertyNameAlias = propertyToBeRemoved.Name; PropertyConfiguration propertyConfiguration; entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo); if (propertyToBeRemoved.Multiplicity == EdmMultiplicity.Many) { propertyConfiguration = entityToBePatched.AddCollectionProperty(propertyToBeRemoved.PropertyInfo); } else { propertyConfiguration = entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo); } Contract.Assert(propertyToBeRemoved.AddedExplicitly == false); // The newly added property must be marked as added implicitly. This can make sure the property // conventions can be re-applied to the new property. propertyConfiguration.AddedExplicitly = false; ReapplyPropertyConvention(propertyConfiguration, entityToBePatched); propertyConfiguration.Name = propertyNameAlias; } } } }
internal void ReconfigInferedEntityTypeAsComplexType(Type propertyType) { HashSet <Type> visitedTypes = new HashSet <Type>(); Queue <Type> typeToBeVisited = new Queue <Type>(); typeToBeVisited.Enqueue(propertyType); IList <EntityTypeConfiguration> foundMappedTypes = new List <EntityTypeConfiguration>(); while (typeToBeVisited.Count != 0) { Type currentType = typeToBeVisited.Dequeue(); visitedTypes.Add(currentType); List <Type> derivedTypes; if (_allTypesWithDerivedTypeMapping.Value.TryGetValue(currentType, out derivedTypes)) { foreach (Type derivedType in derivedTypes) { if (!visitedTypes.Contains(derivedType)) { StructuralTypeConfiguration structuralType = StructuralTypes.Except(_explicitlyAddedTypes) .FirstOrDefault(c => c.ClrType == derivedType); if (structuralType != null && structuralType.Kind == EdmTypeKind.Entity) { foundMappedTypes.Add((EntityTypeConfiguration)structuralType); } typeToBeVisited.Enqueue(derivedType); } } } } if (foundMappedTypes.Any()) { ReconfigureEntityTypesAsComplexType(foundMappedTypes.ToArray()); } }
private void ReconfigureEntityTypesAsComplexType(EntityTypeConfiguration[] misconfiguredEntityTypes) { IEnumerable <EntityTypeConfiguration> actualEntityTypes = StructuralTypes .Except(misconfiguredEntityTypes) .OfType <EntityTypeConfiguration>(); foreach (EntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes) { RemoveStructuralType(misconfiguredEntityType.ClrType); // this is a wrongly inferred type. so just ignore any pending configuration from it. AddComplexType(misconfiguredEntityType.ClrType); foreach (EntityTypeConfiguration entityToBePatched in actualEntityTypes) { NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched .NavigationProperties .Where(navigationProperty => navigationProperty.RelatedClrType == misconfiguredEntityType.ClrType) .ToArray(); foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved) { string propertyNameAlias = propertyToBeRemoved.Name; PropertyConfiguration propertyConfiguration; entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo); if (propertyToBeRemoved.Multiplicity == EdmMultiplicity.Many) { propertyConfiguration = entityToBePatched.AddCollectionProperty(propertyToBeRemoved.PropertyInfo); } else { propertyConfiguration = entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo); } propertyConfiguration.Name = propertyNameAlias; } } } }
private void ReconfigureEntityTypesAsComplexType(IEnumerable <EntityTypeConfiguration> misconfiguredEntityTypes) { IEnumerable <EntityTypeConfiguration> actualEntityTypes = StructuralTypes .Except(misconfiguredEntityTypes) .OfType <EntityTypeConfiguration>() .ToArray(); foreach (EntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes) { RemoveStructuralType(misconfiguredEntityType.ClrType); ComplexTypeConfiguration newComplexType = AddComplexType(misconfiguredEntityType.ClrType); foreach (PropertyInfo ignoredProperty in misconfiguredEntityType.IgnoredProperties) { newComplexType.RemoveProperty(ignoredProperty); } foreach (EntityTypeConfiguration entityToBePatched in actualEntityTypes) { NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched .NavigationProperties .Where(navigationProperty => navigationProperty.RelatedClrType == misconfiguredEntityType.ClrType) .ToArray(); foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved) { entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo); if (propertyToBeRemoved.Multiplicity == EdmMultiplicity.Many) { entityToBePatched.AddCollectionProperty(propertyToBeRemoved.PropertyInfo); } else { entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo); } } } } }
private void ReconfigureEntityTypesAsComplexType(IEnumerable <IEntityTypeConfiguration> misconfiguredEntityTypes) { IEnumerable <IEntityTypeConfiguration> actualEntityTypes = StructuralTypes .Except(misconfiguredEntityTypes) .OfType <IEntityTypeConfiguration>() .ToArray(); foreach (IEntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes) { RemoveStructuralType(misconfiguredEntityType.ClrType); IComplexTypeConfiguration newComplexType = AddComplexType(misconfiguredEntityType.ClrType); foreach (var ignoredProperty in misconfiguredEntityType.IgnoredProperties) { newComplexType.RemoveProperty(ignoredProperty); } foreach (IEntityTypeConfiguration entityToBePatched in actualEntityTypes) { NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched .NavigationProperties .Where(navigationProperty => navigationProperty.RelatedClrType == misconfiguredEntityType.ClrType) .ToArray(); foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved) { if (propertyToBeRemoved.Multiplicity == EdmMultiplicity.Many) { // complex collections are not supported. throw Error.NotSupported(SRResources.CollectionPropertiesNotSupported, propertyToBeRemoved.PropertyInfo.Name, propertyToBeRemoved.PropertyInfo.ReflectedType.FullName); } entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo); entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo); } } AddComplexType(misconfiguredEntityType.ClrType); } }
private void ReconfigureEntityTypesAsComplexType(EntityTypeConfiguration[] misconfiguredEntityTypes) { IList <EntityTypeConfiguration> actualEntityTypes = StructuralTypes.Except(misconfiguredEntityTypes).OfType <EntityTypeConfiguration>().ToList(); HashSet <EntityTypeConfiguration> visitedEntityType = new HashSet <EntityTypeConfiguration>(); foreach (EntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes) { if (visitedEntityType.Contains(misconfiguredEntityType)) { continue; } // If one of the base types is already configured as entity type, we should keep this type as entity type. IEnumerable <EntityTypeConfiguration> basedTypes = misconfiguredEntityType .BaseTypes().OfType <EntityTypeConfiguration>(); if (actualEntityTypes.Any(e => basedTypes.Any(a => a.ClrType == e.ClrType))) { visitedEntityType.Add(misconfiguredEntityType); continue; } // Make sure to remove current type and all the derived types IList <EntityTypeConfiguration> thisAndDerivedTypes = this.DerivedTypes(misconfiguredEntityType) .Concat(new[] { misconfiguredEntityType }).OfType <EntityTypeConfiguration>().ToList(); foreach (EntityTypeConfiguration subEnityType in thisAndDerivedTypes) { if (actualEntityTypes.Any(e => e.ClrType == subEnityType.ClrType)) { throw Error.InvalidOperation(SRResources.CannotReconfigEntityTypeAsComplexType, misconfiguredEntityType.ClrType.FullName, subEnityType.ClrType.FullName); } RemoveStructuralType(subEnityType.ClrType); } // this is a wrongly inferred type. so just ignore any pending configuration from it. AddComplexType(misconfiguredEntityType.ClrType); foreach (EntityTypeConfiguration subEnityType in thisAndDerivedTypes) { visitedEntityType.Add(subEnityType); foreach (EntityTypeConfiguration entityToBePatched in actualEntityTypes) { NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched .NavigationProperties .Where(navigationProperty => navigationProperty.RelatedClrType == subEnityType.ClrType) .ToArray(); foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved) { string propertyNameAlias = propertyToBeRemoved.Name; PropertyConfiguration propertyConfiguration; entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo); if (propertyToBeRemoved.Multiplicity == EdmMultiplicity.Many) { propertyConfiguration = entityToBePatched.AddCollectionProperty(propertyToBeRemoved.PropertyInfo); } else { propertyConfiguration = entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo); } Contract.Assert(propertyToBeRemoved.AddedExplicitly == false); // The newly added property must be marked as added implicitly. This can make sure the property // conventions can be re-applied to the new property. propertyConfiguration.AddedExplicitly = false; ReapplyPropertyConvention(propertyConfiguration, entityToBePatched); propertyConfiguration.Name = propertyNameAlias; } } } } }