public void GetRootType_should_return_base_type_when_has_base_type() { var entityType = new EntityType("E", "N", DataSpace.CSpace) { BaseType = new EntityType("E", "N", DataSpace.CSpace) }; Assert.Same(entityType.BaseType, entityType.GetRootType()); }
public void Generate(EntityType entityType, DbDatabaseMapping databaseMapping) { DebugCheck.NotNull(entityType); DebugCheck.NotNull(databaseMapping); var entitySet = databaseMapping.Model.GetEntitySet(entityType); var entitySetMapping = databaseMapping.GetEntitySetMapping(entitySet) ?? databaseMapping.AddEntitySetMapping(entitySet); var entityTypeMapping = entitySetMapping.EntityTypeMappings.FirstOrDefault( m => m.EntityTypes.Contains(entitySet.ElementType)) ?? entitySetMapping.EntityTypeMappings.FirstOrDefault(); var table = entityTypeMapping != null ? entityTypeMapping.MappingFragments.First().Table : databaseMapping.Database.AddTable(entityType.GetRootType().Name); entityTypeMapping = new EntityTypeMapping(null); var entityTypeMappingFragment = new MappingFragment(databaseMapping.Database.GetEntitySet(table), entityTypeMapping, false); entityTypeMapping.AddType(entityType); entityTypeMapping.AddFragment(entityTypeMappingFragment); entityTypeMapping.SetClrType(entityType.GetClrType()); entitySetMapping.AddTypeMapping(entityTypeMapping); new PropertyMappingGenerator(_providerManifest) .Generate( entityType, entityType.Properties, entitySetMapping, entityTypeMappingFragment, new List<EdmProperty>(), false); }
public void Generate( EntityType entityType, IEnumerable<EdmProperty> properties, EntitySetMapping entitySetMapping, MappingFragment entityTypeMappingFragment, IList<EdmProperty> propertyPath, bool createNewColumn) { DebugCheck.NotNull(entityType); DebugCheck.NotNull(properties); DebugCheck.NotNull(entityTypeMappingFragment); DebugCheck.NotNull(propertyPath); var rootDeclaredProperties = entityType.GetRootType().DeclaredProperties; foreach (var property in properties) { if (property.IsComplexType && propertyPath.Any( p => p.IsComplexType && (p.ComplexType == property.ComplexType))) { throw Error.CircularComplexTypeHierarchy(); } propertyPath.Add(property); if (property.IsComplexType) { Generate( entityType, property.ComplexType.Properties, entitySetMapping, entityTypeMappingFragment, propertyPath, createNewColumn); } else { var tableColumn = entitySetMapping.EntityTypeMappings .SelectMany(etm => etm.MappingFragments) .SelectMany(etmf => etmf.ColumnMappings) .Where(pm => pm.PropertyPath.SequenceEqual(propertyPath)) .Select(pm => pm.ColumnProperty) .FirstOrDefault(); if (tableColumn == null || createNewColumn) { var columnName = string.Join("_", propertyPath.Select(p => p.Name)); tableColumn = MapTableColumn( property, columnName, !rootDeclaredProperties.Contains(propertyPath.First())); entityTypeMappingFragment.Table.AddColumn(tableColumn); if (entityType.KeyProperties().Contains(property)) { entityTypeMappingFragment.Table.AddKeyMember(tableColumn); } } entityTypeMappingFragment.AddColumnMapping( new ColumnMappingBuilder(tableColumn, propertyPath.ToList())); } propertyPath.Remove(property); } }
public static EntitySet GetEntitySet(this EdmModel model, EntityType entityType) { DebugCheck.NotNull(model); DebugCheck.NotNull(entityType); Debug.Assert(model.Containers.Count() == 1); return model.GetEntitySets().SingleOrDefault(e => e.ElementType == entityType.GetRootType()); }
private static bool UpdateColumnNamesForTableSharing( DbDatabaseMapping databaseMapping, EntityType entityType, EntityType toTable, StorageMappingFragment fragment) { // Validate: this table can be used only if: // 1. The table is not used by any other type // 2. The table is used only by types in the same type hierarchy (TPH) // 3. There is a 1:1 relationship and the PK count and types match (Table Splitting) var typesSharingTable = FindAllTypesUsingTable(databaseMapping, toTable); var associationsToSharedTable = new Dictionary<EntityType, List<AssociationType>>(); foreach (var candidateType in typesSharingTable) { var oneToOneAssocations = FindAllOneToOneFKAssociationTypes( databaseMapping.Model, entityType, candidateType); var rootType = candidateType.GetRootType(); if (!associationsToSharedTable.ContainsKey(rootType)) { associationsToSharedTable.Add(rootType, oneToOneAssocations.ToList()); } else { associationsToSharedTable[rootType].AddRange(oneToOneAssocations); } } foreach (var candidateTypePair in associationsToSharedTable) { // Check if these types are in a TPH hierarchy if (candidateTypePair.Key != entityType.GetRootType() && candidateTypePair.Value.Count == 0) { var tableName = toTable.GetTableName(); throw Error.EntityMappingConfiguration_InvalidTableSharing( entityType.Name, candidateTypePair.Key.Name, tableName != null ? tableName.Name : databaseMapping.Database.GetEntitySet(toTable).Table); } } var allAssociations = associationsToSharedTable.Values.SelectMany(l => l); if (allAssociations.Any()) { var principalKeyNamesType = toTable.GetKeyNamesType(); if (principalKeyNamesType == null) { // grab a candidate var association = allAssociations.First(); principalKeyNamesType = association.Constraint.FromRole.GetEntityType(); if (allAssociations.All(x => x.Constraint.FromRole.GetEntityType() == principalKeyNamesType)) { toTable.SetKeyNamesType(principalKeyNamesType); } } // rename the columns in the fragment to match the principal keys var principalKeys = principalKeyNamesType.KeyProperties().ToArray(); var i = 0; foreach (var k in entityType.KeyProperties()) { var dependentColumn = fragment.ColumnMappings.Single(pm => pm.PropertyPath.First() == k).ColumnProperty; dependentColumn.Name = principalKeys[i].Name; i++; } return true; } return false; }
private void ConfigureKey(EntityType entityType) { DebugCheck.NotNull(entityType); if (!_keyProperties.Any()) { return; } if (entityType.BaseType != null) { throw Error.KeyRegisteredOnDerivedType(ClrType, entityType.GetRootType().GetClrType()); } var keyProperties = _keyProperties.AsEnumerable(); if (!_isKeyConfigured) { var primaryKeys = from p in _keyProperties select new { PropertyInfo = p, Property(new PropertyPath(p)).ColumnOrder }; if ((_keyProperties.Count > 1) && primaryKeys.Any(p => !p.ColumnOrder.HasValue)) { throw Error.ModelGeneration_UnableToDetermineKeyOrder(ClrType); } keyProperties = primaryKeys.OrderBy(p => p.ColumnOrder).Select(p => p.PropertyInfo); } foreach (var keyProperty in keyProperties) { var property = entityType.GetDeclaredPrimitiveProperty(keyProperty); if (property == null) { throw Error.KeyPropertyNotFound(keyProperty.Name, entityType.Name); } property.Nullable = false; entityType.AddKeyMember(property); } }
public static EntitySet GetEntitySet(this EdmModel model, System.Data.Entity.Core.Metadata.Edm.EntityType entityType) { return(model.GetEntitySets().SingleOrDefault <EntitySet>((Func <EntitySet, bool>)(e => e.ElementType == entityType.GetRootType()))); }