public void WriteMappingFragment_should_write_store_entity_set_name() { var fixture = new Fixture(); var entityType = new EntityType("E", "N", DataSpace.CSpace); var entitySet = new EntitySet("ES", "S", null, null, entityType); var entityContainer = new EntityContainer("EC", DataSpace.SSpace); entityContainer.AddEntitySetBase(entitySet); var storageEntitySetMapping = new StorageEntitySetMapping( entitySet, new StorageEntityContainerMapping(entityContainer)); StorageTypeMapping typeMapping = new StorageEntityTypeMapping(storageEntitySetMapping); var mappingFragment = new StorageMappingFragment(entitySet, typeMapping, false); fixture.Writer.WriteMappingFragmentElement(mappingFragment); Assert.Equal( @"<MappingFragment StoreEntitySet=""ES"" />", fixture.ToString()); }
private void RemoveFragment( EntitySet entitySet, StorageEntityTypeMapping entityTypeMapping, StorageMappingFragment fragment) { // Make the default discriminator nullable if this type isn't using it but there is a base type var defaultDiscriminator = fragment.GetDefaultDiscriminator(); if (defaultDiscriminator != null && entityTypeMapping.EntityType.BaseType != null) { var columnMapping = _tableMappings[fragment.Table].ColumnMappings.SingleOrDefault( cm => cm.Column == defaultDiscriminator); if (columnMapping != null) { var propertyMapping = columnMapping.PropertyMappings.SingleOrDefault( pm => pm.EntityType == entityTypeMapping.EntityType); if (propertyMapping != null) { columnMapping.PropertyMappings.Remove(propertyMapping); } } defaultDiscriminator.Nullable = true; } entityTypeMapping.RemoveFragment(fragment); if (!entityTypeMapping.MappingFragments.Any()) { _databaseMapping.GetEntitySetMapping(entitySet).RemoveTypeMapping(entityTypeMapping); } }
public void AddEntityTypeMappingFragment( EntitySet entitySet, EntityType entityType, StorageMappingFragment fragment) { Debug.Assert(fragment.Table == Table); _entityTypes.Add(entitySet, entityType); var defaultDiscriminatorColumn = fragment.GetDefaultDiscriminator(); StorageConditionPropertyMapping defaultDiscriminatorCondition = null; if (defaultDiscriminatorColumn != null) { defaultDiscriminatorCondition = fragment.ColumnConditions.SingleOrDefault(cc => cc.ColumnProperty == defaultDiscriminatorColumn); } foreach (var pm in fragment.ColumnMappings) { var columnMapping = FindOrCreateColumnMapping(pm.ColumnProperty); columnMapping.AddMapping( entityType, pm.PropertyPath, fragment.ColumnConditions.Where(cc => cc.ColumnProperty == pm.ColumnProperty), defaultDiscriminatorColumn == pm.ColumnProperty); } // Add any column conditions that aren't mapped to properties foreach ( var cc in fragment.ColumnConditions.Where(cc => !fragment.ColumnMappings.Any(pm => pm.ColumnProperty == cc.ColumnProperty))) { var columnMapping = FindOrCreateColumnMapping(cc.ColumnProperty); columnMapping.AddMapping(entityType, null, new[] { cc }, defaultDiscriminatorColumn == cc.ColumnProperty); } }
public static void RemoveDefaultDiscriminatorAnnotation( this StorageMappingFragment entityTypeMappingFragment) { DebugCheck.NotNull(entityTypeMappingFragment); entityTypeMappingFragment.Annotations.RemoveAnnotation(DefaultDiscriminatorAnnotation); }
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); }
private static StorageMappingFragment FindConditionTypeMappingFragment( EntitySet tableSet, StorageMappingFragment propertiesTypeMappingFragment, StorageEntityTypeMapping conditionTypeMapping) { var table = tableSet.ElementType; var conditionTypeMappingFragment = conditionTypeMapping.MappingFragments .SingleOrDefault(x => x.Table == table); if (conditionTypeMappingFragment == null) { conditionTypeMappingFragment = EntityMappingOperations .CreateTypeMappingFragment(conditionTypeMapping, propertiesTypeMappingFragment, tableSet); conditionTypeMappingFragment.SetIsConditionOnlyFragment(true); if (propertiesTypeMappingFragment.GetDefaultDiscriminator() != null) { conditionTypeMappingFragment.SetDefaultDiscriminator( propertiesTypeMappingFragment.GetDefaultDiscriminator()); propertiesTypeMappingFragment.RemoveDefaultDiscriminatorAnnotation(); } } return(conditionTypeMappingFragment); }
public static void RemoveDefaultDiscriminator( this StorageMappingFragment entityTypeMappingFragment, StorageEntitySetMapping entitySetMapping) { DebugCheck.NotNull(entityTypeMappingFragment); var discriminatorColumn = entityTypeMappingFragment.RemoveDefaultDiscriminatorCondition(); if (discriminatorColumn != null) { var table = entityTypeMappingFragment.Table; table.Properties .Where(c => c.Name.Equals(discriminatorColumn.Name, StringComparison.Ordinal)) .ToList() .Each(table.RemoveMember); } if (entitySetMapping != null && entityTypeMappingFragment.IsConditionOnlyFragment() && !entityTypeMappingFragment.ColumnConditions.Any()) { var entityTypeMapping = entitySetMapping.EntityTypeMappings.Single( etm => etm.MappingFragments.Contains(entityTypeMappingFragment)); entityTypeMapping.RemoveFragment(entityTypeMappingFragment); if (entityTypeMapping.MappingFragments.Count == 0) { entitySetMapping.RemoveTypeMapping(entityTypeMapping); } } }
public static void SetDefaultDiscriminator( this StorageMappingFragment entityTypeMappingFragment, EdmProperty discriminator) { DebugCheck.NotNull(entityTypeMappingFragment); entityTypeMappingFragment.Annotations.SetAnnotation(DefaultDiscriminatorAnnotation, discriminator); }
public static EdmProperty GetDefaultDiscriminator( this StorageMappingFragment entityTypeMapppingFragment) { DebugCheck.NotNull(entityTypeMapppingFragment); return ((EdmProperty) entityTypeMapppingFragment.Annotations.GetAnnotation(DefaultDiscriminatorAnnotation)); }
public static void CopyPropertyMappingToFragment( ColumnMappingBuilder propertyMappingBuilder, StorageMappingFragment fragment, bool useExisting) { // Ensure column is in the fragment's table var column = TablePrimitiveOperations.IncludeColumn(fragment.Table, propertyMappingBuilder.ColumnProperty, useExisting); // Add the property mapping fragment.AddColumnMapping( new ColumnMappingBuilder(column, propertyMappingBuilder.PropertyPath)); }
/// <summary> /// Gets all model properties mapped to server generated columns. /// </summary> private static IEnumerable <EdmMember> FindServerGenMembers(StorageMappingFragment mappingFragment) { foreach (var scalarPropertyMapping in FlattenPropertyMappings(mappingFragment.AllProperties) .OfType <StorageScalarPropertyMapping>()) { if (StoreGeneratedPattern.None != MetadataHelper.GetStoreGeneratedPattern(scalarPropertyMapping.ColumnProperty)) { yield return(scalarPropertyMapping.EdmProperty); } } }
/// <summary> /// Gets all store columns participating in is null conditions. /// </summary> private static IEnumerable <EdmMember> FindIsNullConditionColumns(StorageMappingFragment mappingFragment) { foreach (var conditionPropertyMapping in FlattenPropertyMappings(mappingFragment.AllProperties) .OfType <StorageConditionPropertyMapping>()) { if (conditionPropertyMapping.ColumnProperty != null && conditionPropertyMapping.IsNull.HasValue) { yield return(conditionPropertyMapping.ColumnProperty); } } }
public static void AddNullabilityCondition( this StorageMappingFragment entityTypeMapppingFragment, EdmProperty column, bool isNull) { DebugCheck.NotNull(entityTypeMapppingFragment); DebugCheck.NotNull(column); entityTypeMapppingFragment .AddConditionProperty( new StorageConditionPropertyMapping(null, column, null, isNull)); }
public static void AddDiscriminatorCondition( this StorageMappingFragment entityTypeMapppingFragment, EdmProperty discriminatorColumn, object value) { DebugCheck.NotNull(entityTypeMapppingFragment); DebugCheck.NotNull(discriminatorColumn); DebugCheck.NotNull(value); entityTypeMapppingFragment .AddConditionProperty( new StorageConditionPropertyMapping(null, discriminatorColumn, value, null)); }
public static void UpdatePropertyMappings( EdmModel database, EntityType fromTable, StorageMappingFragment fragment, bool useExisting) { // move the column from the formTable to the table in fragment if (fromTable != fragment.Table) { fragment.ColumnMappings.Each( pm => UpdatePropertyMapping(database, pm, fromTable, fragment.Table, useExisting)); } }
public static bool IsUnmappedPropertiesFragment(this StorageMappingFragment entityTypeMapppingFragment) { DebugCheck.NotNull(entityTypeMapppingFragment); var isUnmappedPropertiesFragment = entityTypeMapppingFragment.Annotations.GetAnnotation(UnmappedPropertiesFragmentAnnotation); if (isUnmappedPropertiesFragment != null) { return((bool)isUnmappedPropertiesFragment); } return(false); }
public static bool IsConditionOnlyFragment(this StorageMappingFragment entityTypeMapppingFragment) { DebugCheck.NotNull(entityTypeMapppingFragment); var isConditionOnlyFragment = entityTypeMapppingFragment.Annotations.GetAnnotation(ConditionOnlyFragmentAnnotation); if (isConditionOnlyFragment != null) { return((bool)isConditionOnlyFragment); } return(false); }
private void ConfigureDefaultDiscriminator( EntityType entityType, StorageMappingFragment fragment) { if (ValueConditions.Any() || NullabilityConditions.Any()) { var discriminator = fragment.RemoveDefaultDiscriminatorCondition(); if (discriminator != null && entityType.BaseType != null) { discriminator.Nullable = true; } } }
public static void SetIsConditionOnlyFragment( this StorageMappingFragment entityTypeMapppingFragment, bool isConditionOnlyFragment) { DebugCheck.NotNull(entityTypeMapppingFragment); if (isConditionOnlyFragment) { entityTypeMapppingFragment.Annotations.SetAnnotation( ConditionOnlyFragmentAnnotation, isConditionOnlyFragment); } else { entityTypeMapppingFragment.Annotations.RemoveAnnotation(ConditionOnlyFragmentAnnotation); } }
public static void UpdateConditions( EdmModel database, EntityType fromTable, StorageMappingFragment fragment) { // move the condition's column from the formTable to the table in fragment if (fromTable != fragment.Table) { fragment.ColumnConditions.Each( cc => { cc.ColumnProperty = TableOperations.CopyColumnAndAnyConstraints( database, fromTable, fragment.Table, cc.ColumnProperty, true, false); }); } }
public static void SetIsUnmappedPropertiesFragment( this StorageMappingFragment entityTypeMapppingFragment, bool isUnmappedPropertiesFragment) { DebugCheck.NotNull(entityTypeMapppingFragment); if (isUnmappedPropertiesFragment) { entityTypeMapppingFragment.Annotations.SetAnnotation( UnmappedPropertiesFragmentAnnotation, isUnmappedPropertiesFragment); } else { entityTypeMapppingFragment.Annotations.RemoveAnnotation(UnmappedPropertiesFragmentAnnotation); } }
public static StorageMappingFragment CreateTypeMappingFragment( StorageEntityTypeMapping entityTypeMapping, StorageMappingFragment templateFragment, EntitySet tableSet) { var fragment = new StorageMappingFragment(tableSet, entityTypeMapping, false); entityTypeMapping.AddFragment(fragment); // Move all PK mappings to the extra fragment foreach ( var pkPropertyMapping in templateFragment.ColumnMappings.Where(pm => pm.ColumnProperty.IsPrimaryKeyColumn)) { CopyPropertyMappingToFragment(pkPropertyMapping, fragment, true); } return(fragment); }
private void WriteMappingFragmentElement(StorageMappingFragment mappingFragment) { _xmlWriter.WriteStartElement(StorageMslConstructs.MappingFragmentElement); _xmlWriter.WriteAttributeString(StorageMslConstructs.MappingFragmentStoreEntitySetAttribute, mappingFragment.Table.Name); foreach (var propertyMapping in mappingFragment.Properties) { WritePropertyMapping(propertyMapping); } foreach (var conditionColumn in mappingFragment.ColumnConditions) { WriteConditionElement(conditionColumn); } _xmlWriter.WriteEndElement(); }
private static void MoveDefaultDiscriminator( StorageMappingFragment fromFragment, StorageMappingFragment toFragment) { var discriminatorColumn = fromFragment.GetDefaultDiscriminator(); if (discriminatorColumn != null) { var discriminator = fromFragment.ColumnConditions.SingleOrDefault( cc => cc.ColumnProperty == discriminatorColumn); if (discriminator != null) { fromFragment.RemoveDefaultDiscriminatorAnnotation(); fromFragment.RemoveConditionProperty(discriminator); toFragment.AddDiscriminatorCondition(discriminator.ColumnProperty, discriminator.Value); toFragment.SetDefaultDiscriminator(discriminator.ColumnProperty); } } }
public static void MovePropertyMapping( EdmModel database, StorageMappingFragment fromFragment, StorageMappingFragment toFragment, ColumnMappingBuilder propertyMappingBuilder, bool requiresUpdate, bool useExisting) { // move the column from the formTable to the table in fragment if (requiresUpdate && fromFragment.Table != toFragment.Table) { UpdatePropertyMapping(database, propertyMappingBuilder, fromFragment.Table, toFragment.Table, useExisting); } // move the propertyMapping fromFragment.RemoveColumnMapping(propertyMappingBuilder); toFragment.AddColumnMapping(propertyMappingBuilder); }
public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type() { var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace)); var entitySet = new EntitySet { Name = "ES" }; var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet); var entityTypeMapping = new StorageEntityTypeMapping(null); entitySetMapping.AddTypeMapping(entityTypeMapping); var entityTypeMappingFragment = new StorageMappingFragment(entitySet, entityTypeMapping, false); entityTypeMapping.AddFragment(entityTypeMappingFragment); var complexType = new ComplexType("C"); var propertyMapping1 = new ColumnMappingBuilder( new EdmProperty("C"), new[] { EdmProperty.Complex("P1", complexType), EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)) }); var type = typeof(object); complexType.Annotations.SetClrType(type); entityTypeMappingFragment.AddColumnMapping(propertyMapping1); var propertyMapping2 = new ColumnMappingBuilder( new EdmProperty("C"), new List <EdmProperty> { EdmProperty.Complex("P3", complexType), EdmProperty.Primitive( "P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)), }); entityTypeMappingFragment.AddColumnMapping(propertyMapping2); Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count()); }
public static EdmProperty RemoveDefaultDiscriminatorCondition( this StorageMappingFragment entityTypeMappingFragment) { DebugCheck.NotNull(entityTypeMappingFragment); var discriminatorColumn = entityTypeMappingFragment.GetDefaultDiscriminator(); if (discriminatorColumn != null && entityTypeMappingFragment.ColumnConditions.Any()) { Debug.Assert(entityTypeMappingFragment.ColumnConditions.Count() == 1); entityTypeMappingFragment.ClearConditions(); } entityTypeMappingFragment.RemoveDefaultDiscriminatorAnnotation(); return(discriminatorColumn); }
internal void Configure( DbDatabaseMapping databaseMapping, StorageMappingFragment fragment, EntityType entityType) { DebugCheck.NotNull(fragment); var edmPropertyPath = EntityMappingConfiguration.PropertyPathToEdmPropertyPath(PropertyPath, entityType); if (edmPropertyPath.Count() > 1) { throw Error.InvalidNotNullCondition(PropertyPath.ToString(), entityType.Name); } var column = fragment.ColumnMappings .Where(pm => pm.PropertyPath.SequenceEqual(edmPropertyPath.Single())) .Select(pm => pm.ColumnProperty) .SingleOrDefault(); if (column == null || !fragment.Table.Properties.Contains(column)) { throw Error.InvalidNotNullCondition(PropertyPath.ToString(), entityType.Name); } if (ValueConditionConfiguration.AnyBaseTypeToTableWithoutColumnCondition( databaseMapping, entityType, fragment.Table, column)) { column.Nullable = true; } // Make the property required var newConfiguration = new PrimitivePropertyConfiguration { IsNullable = false, OverridableConfigurationParts = OverridableConfigurationParts.OverridableInSSpace }; newConfiguration.Configure(edmPropertyPath.Single().Last()); fragment.AddNullabilityCondition(column, isNull: false); }
private EntityType FindOrCreateTargetTable( DbDatabaseMapping databaseMapping, StorageMappingFragment fragment, EntityType entityType, EntityType fromTable, out bool isTableSharing) { EntityType toTable; isTableSharing = false; if (TableName == null) { toTable = fragment.Table; } else { toTable = databaseMapping.Database.FindTableByName(TableName); if (toTable == null) { if (entityType.BaseType == null) { // Rule: base type's always own the fragment's initial table toTable = fragment.Table; } else { toTable = databaseMapping.Database.AddTable(TableName.Name, fromTable); } } // Validate this table can be used and update as needed if it is isTableSharing = UpdateColumnNamesForTableSharing(databaseMapping, entityType, toTable, fragment); fragment.TableSet = databaseMapping.Database.GetEntitySet(toTable); toTable.SetTableName(TableName); } return(toTable); }
// effects: Given an extent's ("extent") table fragment that is // contained inside typeMap, determine the cells that need to be // created and add them to cells // allTypes corresponds to all the different types that the type map // represents -- this parameter has something useful only if extent // is an entity set private void ExtractCellsFromTableFragment( EntitySetBase extent, StorageMappingFragment fragmentMap, Set <EdmType> allTypes, List <Cell> cells) { // create C-query components var cRootExtent = new MemberPath(extent); var cQueryWhereClause = BoolExpression.True; var cSlots = new List <ProjectedSlot>(); if (allTypes.Count > 0) { // Create a type condition for the extent, i.e., "extent in allTypes" cQueryWhereClause = BoolExpression.CreateLiteral(new TypeRestriction(cRootExtent, allTypes), null); } // create S-query components var sRootExtent = new MemberPath(fragmentMap.TableSet); var sQueryWhereClause = BoolExpression.True; var sSlots = new List <ProjectedSlot>(); // Association or entity set // Add the properties and the key properties to a list and // then process them in ExtractProperties ExtractProperties( fragmentMap.AllProperties, cRootExtent, cSlots, ref cQueryWhereClause, sRootExtent, sSlots, ref sQueryWhereClause); // limitation of MSL API: cannot assign constant values to table columns var cQuery = new CellQuery(cSlots, cQueryWhereClause, cRootExtent, CellQuery.SelectDistinct.No /*no distinct flag*/); var sQuery = new CellQuery( sSlots, sQueryWhereClause, sRootExtent, fragmentMap.IsSQueryDistinct ? CellQuery.SelectDistinct.Yes : CellQuery.SelectDistinct.No); var fragmentInfo = fragmentMap; Debug.Assert((fragmentInfo != null), "CSMappingFragment should support Line Info"); var label = new CellLabel(fragmentInfo); var cell = Cell.CreateCS(cQuery, sQuery, label, m_currentCellNumber); m_currentCellNumber++; cells.Add(cell); }
internal CellLabel(StorageMappingFragment fragmentInfo) : this(fragmentInfo.StartLineNumber, fragmentInfo.StartLinePosition, fragmentInfo.SourceLocation) { }
// effects: Given an extent's ("extent") table fragment that is // contained inside typeMap, determine the cells that need to be // created and add them to cells // allTypes corresponds to all the different types that the type map // represents -- this parameter has something useful only if extent // is an entity set private void ExtractCellsFromTableFragment( EntitySetBase extent, StorageMappingFragment fragmentMap, Set<EdmType> allTypes, List<Cell> cells) { // create C-query components var cRootExtent = new MemberPath(extent); var cQueryWhereClause = BoolExpression.True; var cSlots = new List<ProjectedSlot>(); if (allTypes.Count > 0) { // Create a type condition for the extent, i.e., "extent in allTypes" cQueryWhereClause = BoolExpression.CreateLiteral(new TypeRestriction(cRootExtent, allTypes), null); } // create S-query components var sRootExtent = new MemberPath(fragmentMap.TableSet); var sQueryWhereClause = BoolExpression.True; var sSlots = new List<ProjectedSlot>(); // Association or entity set // Add the properties and the key properties to a list and // then process them in ExtractProperties ExtractProperties( fragmentMap.AllProperties, cRootExtent, cSlots, ref cQueryWhereClause, sRootExtent, sSlots, ref sQueryWhereClause); // limitation of MSL API: cannot assign constant values to table columns var cQuery = new CellQuery(cSlots, cQueryWhereClause, cRootExtent, CellQuery.SelectDistinct.No /*no distinct flag*/); var sQuery = new CellQuery( sSlots, sQueryWhereClause, sRootExtent, fragmentMap.IsSQueryDistinct ? CellQuery.SelectDistinct.Yes : CellQuery.SelectDistinct.No); var fragmentInfo = fragmentMap; Debug.Assert((fragmentInfo != null), "CSMappingFragment should support Line Info"); var label = new CellLabel(fragmentInfo); var cell = Cell.CreateCS(cQuery, sQuery, label, m_currentCellNumber); m_currentCellNumber++; cells.Add(cell); }