protected static StorageEntityTypeMapping GetEntityTypeMappingInHierarchy( DbDatabaseMapping databaseMapping, EntityType entityType) { DebugCheck.NotNull(databaseMapping); DebugCheck.NotNull(entityType); var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType); if (entityTypeMapping == null) { var entitySetMapping = databaseMapping.GetEntitySetMapping(databaseMapping.Model.GetEntitySet(entityType)); if (entitySetMapping != null) { entityTypeMapping = entitySetMapping .EntityTypeMappings .First( etm => entityType.DeclaredProperties.All( dp => etm.MappingFragments.First() .ColumnMappings.Select(pm => pm.PropertyPath.First()).Contains(dp))); } } if (entityTypeMapping == null) { throw Error.UnmappedAbstractType(entityType.GetClrType()); } return entityTypeMapping; }
private static void UpdatePrincipalTables( DbDatabaseMapping databaseMapping, EntityType toTable, bool removeFks, AssociationType associationType, EntityType et) { AssociationEndMember principalEnd, dependentEnd; var endsToCheck = new List<AssociationEndMember>(); if (associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd)) { endsToCheck.Add(principalEnd); } else if (associationType.SourceEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many && associationType.TargetEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many) { // many to many consider both ends endsToCheck.Add(associationType.SourceEnd); endsToCheck.Add(associationType.TargetEnd); } else { // 1:1 and 0..1:0..1 endsToCheck.Add(associationType.SourceEnd); } foreach (var end in endsToCheck) { if (end.GetEntityType() == et) { IEnumerable<KeyValuePair<EntityType, IEnumerable<EdmProperty>>> dependentTableInfos; if (associationType.Constraint != null) { var originalDependentType = associationType.GetOtherEnd(end).GetEntityType(); var allDependentTypes = databaseMapping.Model.GetSelfAndAllDerivedTypes(originalDependentType); dependentTableInfos = allDependentTypes.Select(t => databaseMapping.GetEntityTypeMapping(t)).Where( dm => dm != null) .SelectMany( dm => dm.MappingFragments .Where( tmf => associationType.Constraint.ToProperties .All( p => tmf.ColumnMappings.Any( pm => pm.PropertyPath.First() == p)))) .Distinct((f1, f2) => f1.Table == f2.Table) .Select( df => new KeyValuePair<EntityType, IEnumerable<EdmProperty>>( df.Table, df.ColumnMappings.Where( pm => associationType.Constraint.ToProperties.Contains( pm.PropertyPath.First())).Select( pm => pm.ColumnProperty))); } else { // IA var associationSetMapping = databaseMapping.EntityContainerMappings .Single().AssociationSetMappings .Single(asm => asm.AssociationSet.ElementType == associationType); var dependentTable = associationSetMapping.Table; var propertyMappings = associationSetMapping.SourceEndMapping.AssociationEnd == end ? associationSetMapping.SourceEndMapping.PropertyMappings : associationSetMapping.TargetEndMapping.PropertyMappings; var dependentColumns = propertyMappings.Select(pm => pm.Column); dependentTableInfos = new[] { new KeyValuePair <EntityType, IEnumerable<EdmProperty>>( dependentTable, dependentColumns) }; } foreach (var tableInfo in dependentTableInfos) { foreach ( var fk in tableInfo.Key.ForeignKeyBuilders.Where( fk => fk.DependentColumns.SequenceEqual(tableInfo.Value)).ToArray( )) { if (removeFks) { tableInfo.Key.RemoveForeignKey(fk); } else if (fk.GetAssociationType() == null || fk.GetAssociationType() == associationType) { fk.PrincipalTable = toTable; } } } } } }
private StorageMappingFragment FindOrCreateTypeMappingFragment( DbDatabaseMapping databaseMapping, ref StorageEntityTypeMapping entityTypeMapping, int configurationIndex, EntityType entityType, DbProviderManifest providerManifest) { StorageMappingFragment fragment = null; if (entityTypeMapping == null) { Debug.Assert(entityType.Abstract); new TableMappingGenerator(providerManifest). Generate(entityType, databaseMapping); entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType); configurationIndex = 0; } if (configurationIndex < entityTypeMapping.MappingFragments.Count) { fragment = entityTypeMapping.MappingFragments[configurationIndex]; } else { if (MapInheritedProperties) { throw Error.EntityMappingConfiguration_DuplicateMapInheritedProperties(entityType.Name); } else if (Properties == null) { throw Error.EntityMappingConfiguration_DuplicateMappedProperties(entityType.Name); } else { Properties.Each( p => { if ( PropertyPathToEdmPropertyPath(p, entityType).Any( pp => !entityType.KeyProperties().Contains(pp.First()))) { throw Error.EntityMappingConfiguration_DuplicateMappedProperty( entityType.Name, p.ToString()); } }); } // Special case where they've asked for an extra table related to this type that only will include the PK columns // Uniquify: can be false, always move to a new table var templateTable = entityTypeMapping.MappingFragments[0].Table; var table = databaseMapping.Database.AddTable(templateTable.Name, templateTable); fragment = EntityMappingOperations.CreateTypeMappingFragment( entityTypeMapping, entityTypeMapping.MappingFragments[0], databaseMapping.Database.GetEntitySet(table)); } return fragment; }
private HashSet<EdmPropertyPath> DiscoverAllMappingsToContain( DbDatabaseMapping databaseMapping, EntityType entityType, EntityType toTable, bool isSharingTableWithBase) { // Ensure all specified properties are the only ones present in this fragment and table var mappingsToContain = new HashSet<EdmPropertyPath>(); // Include Key Properties always entityType.KeyProperties().Each( p => mappingsToContain.AddRange(p.ToPropertyPathList())); // Include All Inherited Properties if (MapInheritedProperties) { entityType.Properties.Except(entityType.DeclaredProperties).Each( p => mappingsToContain.AddRange(p.ToPropertyPathList())); } // If sharing table with base type, include all the mappings that the base has if (isSharingTableWithBase) { var baseMappingsToContain = new HashSet<EdmPropertyPath>(); var baseType = (EntityType)entityType.BaseType; StorageEntityTypeMapping baseMapping = null; StorageMappingFragment baseFragment = null; // if the base is abstract it may have no mapping so look upwards until you find either: // 1. a type with mappings and // 2. if none can be found (abstract until the root or hit another table), then include all declared properties on that base type while (baseType != null && baseMapping == null) { baseMapping = databaseMapping.GetEntityTypeMapping((EntityType)entityType.BaseType); if (baseMapping != null) { baseFragment = baseMapping.MappingFragments.SingleOrDefault(tmf => tmf.Table == toTable); } if (baseFragment == null) { baseType.DeclaredProperties.Each( p => baseMappingsToContain.AddRange(p.ToPropertyPathList())); } baseType = (EntityType)baseType.BaseType; } if (baseFragment != null) { foreach (var pm in baseFragment.ColumnMappings) { mappingsToContain.Add(new EdmPropertyPath(pm.PropertyPath)); } } mappingsToContain.AddRange(baseMappingsToContain); } if (Properties == null) { // Include All Declared Properties entityType.DeclaredProperties.Each( p => mappingsToContain.AddRange(p.ToPropertyPathList())); } else { // Include Specific Properties Properties.Each( p => mappingsToContain.AddRange(PropertyPathToEdmPropertyPath(p, entityType))); } return mappingsToContain; }
private void ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) { var sortedEntityConfigurations = SortEntityConfigurationsByInheritance(databaseMapping); foreach (var entityTypeConfiguration in sortedEntityConfigurations) { var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityTypeConfiguration.ClrType); entityTypeConfiguration.ConfigureTablesAndConditions( entityTypeMapping, databaseMapping, providerManifest); // run through all unconfigured derived types of the current entityType to make sure the property mappings now point to the right places ConfigureUnconfiguredDerivedTypes( databaseMapping, providerManifest, databaseMapping.Model.GetEntityType(entityTypeConfiguration.ClrType), sortedEntityConfigurations); } new EntityMappingService(databaseMapping).Configure(); foreach (var entityType in databaseMapping.Model.GetEntityTypes().Where(e => e.GetConfiguration() != null)) { var entityTypeConfiguration = (EntityTypeConfiguration)entityType.GetConfiguration(); entityTypeConfiguration.Configure(entityType, databaseMapping, providerManifest); } }
private static EntityType FindBaseTableForExtraPropertyMapping( DbDatabaseMapping databaseMapping, EntityType entityType, ColumnMappingBuilder pm) { var baseType = (EntityType)entityType.BaseType; StorageMappingFragment baseFragment = null; while (baseType != null && baseFragment == null) { var baseMapping = databaseMapping.GetEntityTypeMapping(baseType); if (baseMapping != null) { baseFragment = baseMapping.MappingFragments.SingleOrDefault( f => f.ColumnMappings.Any(bpm => bpm.PropertyPath.SequenceEqual(pm.PropertyPath))); if (baseFragment != null) { return baseFragment.Table; } } baseType = (EntityType)baseType.BaseType; } return null; }
internal void Configure( EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) { DebugCheck.NotNull(entityType); DebugCheck.NotNull(databaseMapping); DebugCheck.NotNull(providerManifest); var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType.GetClrType()); if (entityTypeMapping != null) { VerifyAllCSpacePropertiesAreMapped( databaseMapping.GetEntityTypeMappings(entityType).ToList(), entityTypeMapping.EntityType.DeclaredProperties, new List<EdmProperty>()); } ConfigurePropertyMappings(databaseMapping, entityType, providerManifest); ConfigureAssociationMappings(databaseMapping, entityType, providerManifest); ConfigureDependentKeys(databaseMapping, providerManifest); ConfigureModificationFunctions(databaseMapping, entityType, providerManifest); }
internal static void ConfigureUnconfiguredType( DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest, EntityType entityType) { var c = new EntityMappingConfiguration(); var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType.GetClrType()); c.Configure(databaseMapping, providerManifest, entityType, ref entityTypeMapping, false, 0, 1); }
internal static void ConfigureUnconfiguredType( DbDatabaseMapping databaseMapping, ICollection<EntitySet> entitySets, DbProviderManifest providerManifest, EntityType entityType, IDictionary<string, object> commonAnnotations) { var c = new EntityMappingConfiguration(); var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType.GetClrType()); c.Configure(databaseMapping, entitySets, providerManifest, entityType, ref entityTypeMapping, false, 0, 1, commonAnnotations); }
public void GetEntityTypeMapping_should_return_mapping_for_type_by_clrType() { var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace)); var entityType = new EntityType("Foo", "N", DataSpace.CSpace); var type = typeof(object); entityType.Annotations.SetClrType(type); var entityTypeMapping = new StorageEntityTypeMapping(null); entityTypeMapping.AddType(entityType); entityTypeMapping.SetClrType(typeof(object)); databaseMapping.AddEntitySetMapping( new EntitySet { Name = "ES" }).AddTypeMapping(entityTypeMapping); Assert.Same(entityTypeMapping, databaseMapping.GetEntityTypeMapping(typeof(object))); }
public void GetEntityTypeMapping_should_return_mapping_for_type() { var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace)); var entityType = new EntityType("E", "N", DataSpace.CSpace); var entityTypeMapping = new StorageEntityTypeMapping(null); entityTypeMapping.AddType(entityType); databaseMapping.AddEntitySetMapping( new EntitySet { Name = "ES" }).AddTypeMapping(entityTypeMapping); Assert.Same(entityTypeMapping, databaseMapping.GetEntityTypeMapping(entityType)); }