private static void MoveAssociationSetMappingDependents( AssociationSetMapping associationSetMapping, EndPropertyMapping dependentMapping, EntitySet toSet, bool useExistingColumns) { DebugCheck.NotNull(associationSetMapping); DebugCheck.NotNull(dependentMapping); DebugCheck.NotNull(toSet); var toTable = toSet.ElementType; dependentMapping.PropertyMappings.Each( pm => { var oldColumn = pm.Column; pm.Column = TableOperations.MoveColumnAndAnyConstraints( associationSetMapping.Table, toTable, oldColumn, useExistingColumns); associationSetMapping.Conditions .Where(cc => cc.Column == oldColumn) .Each(cc => cc.Column = pm.Column); }); associationSetMapping.StoreEntitySet = toSet; }
private static IEnumerable <Tuple <ModificationFunctionMemberPath, EdmProperty> > GetIndependentFkColumns( EntityType entityType, DbDatabaseMapping databaseMapping) { foreach (AssociationSetMapping associationSetMapping in databaseMapping.GetAssociationSetMappings()) { AssociationType associationType = associationSetMapping.AssociationSet.ElementType; if (!associationType.IsManyToMany()) { AssociationEndMember _; AssociationEndMember dependentEnd; if (!associationType.TryGuessPrincipalAndDependentEnds(out _, out dependentEnd)) { dependentEnd = associationType.TargetEnd; } EntityType dependentEntityType = dependentEnd.GetEntityType(); if (dependentEntityType == entityType || ModificationFunctionMappingGenerator.GetParents(entityType).Contains <EntityType>(dependentEntityType)) { EndPropertyMapping endPropertyMapping = associationSetMapping.TargetEndMapping.AssociationEnd != dependentEnd ? associationSetMapping.TargetEndMapping : associationSetMapping.SourceEndMapping; foreach (ScalarPropertyMapping propertyMapping in endPropertyMapping.PropertyMappings) { yield return(Tuple.Create <ModificationFunctionMemberPath, EdmProperty>(new ModificationFunctionMemberPath((IEnumerable <EdmMember>) new EdmMember[2] { (EdmMember)propertyMapping.Property, (EdmMember)dependentEnd }, associationSetMapping.AssociationSet), propertyMapping.Column)); } } } } }
private IEnumerable <EdmProperty> GenerateIndependentForeignKeyColumns( EntityType principalEntityType, EntityType dependentEntityType, AssociationSetMapping associationSetMapping, EndPropertyMapping associationEndMapping, EntityType dependentTable, bool isPrimaryKeyColumn, NavigationProperty principalNavigationProperty) { foreach (EdmProperty keyProperty in principalEntityType.KeyProperties()) { string columnName = (principalNavigationProperty != null ? principalNavigationProperty.Name : principalEntityType.Name) + "_" + keyProperty.Name; EdmProperty foreignKeyColumn = this.MapTableColumn(keyProperty, columnName, false); dependentTable.AddColumn(foreignKeyColumn); if (isPrimaryKeyColumn) { dependentTable.AddKeyMember((EdmMember)foreignKeyColumn); } foreignKeyColumn.Nullable = associationEndMapping.AssociationEnd.IsOptional() || associationEndMapping.AssociationEnd.IsRequired() && dependentEntityType.BaseType != null; foreignKeyColumn.StoreGeneratedPattern = StoreGeneratedPattern.None; yield return(foreignKeyColumn); associationEndMapping.AddPropertyMapping(new ScalarPropertyMapping(keyProperty, foreignKeyColumn)); if (foreignKeyColumn.Nullable) { associationSetMapping.AddCondition((ConditionPropertyMapping) new IsNullConditionMapping(foreignKeyColumn, false)); } } }
private void ExtractProperties( IEnumerable <PropertyMapping> properties, MemberPath cNode, List <ProjectedSlot> cSlots, ref BoolExpression cQueryWhereClause, MemberPath sRootExtent, List <ProjectedSlot> sSlots, ref BoolExpression sQueryWhereClause) { foreach (PropertyMapping property in properties) { ScalarPropertyMapping scalarPropertyMapping = property as ScalarPropertyMapping; ComplexPropertyMapping complexPropertyMapping = property as ComplexPropertyMapping; EndPropertyMapping endPropertyMapping = property as EndPropertyMapping; ConditionPropertyMapping conditionMap = property as ConditionPropertyMapping; if (scalarPropertyMapping != null) { MemberPath node1 = new MemberPath(cNode, (EdmMember)scalarPropertyMapping.Property); MemberPath node2 = new MemberPath(sRootExtent, (EdmMember)scalarPropertyMapping.Column); cSlots.Add((ProjectedSlot) new MemberProjectedSlot(node1)); sSlots.Add((ProjectedSlot) new MemberProjectedSlot(node2)); } if (complexPropertyMapping != null) { foreach (ComplexTypeMapping typeMapping in complexPropertyMapping.TypeMappings) { MemberPath memberPath = new MemberPath(cNode, (EdmMember)complexPropertyMapping.Property); Set <EdmType> set = new Set <EdmType>(); IEnumerable <EdmType> elements = Helpers.AsSuperTypeList <ComplexType, EdmType>((IEnumerable <ComplexType>)typeMapping.Types); set.AddRange(elements); foreach (EdmType isOfType in typeMapping.IsOfTypes) { set.AddRange(MetadataHelper.GetTypeAndSubtypesOf(isOfType, (ItemCollection)this.m_containerMapping.StorageMappingItemCollection.EdmItemCollection, false)); } BoolExpression literal = BoolExpression.CreateLiteral((BoolLiteral) new TypeRestriction(memberPath, (IEnumerable <EdmType>)set), (MemberDomainMap)null); cQueryWhereClause = BoolExpression.CreateAnd(cQueryWhereClause, literal); this.ExtractProperties((IEnumerable <PropertyMapping>)typeMapping.AllProperties, memberPath, cSlots, ref cQueryWhereClause, sRootExtent, sSlots, ref sQueryWhereClause); } } if (endPropertyMapping != null) { MemberPath cNode1 = new MemberPath(cNode, (EdmMember)endPropertyMapping.AssociationEnd); this.ExtractProperties((IEnumerable <PropertyMapping>)endPropertyMapping.PropertyMappings, cNode1, cSlots, ref cQueryWhereClause, sRootExtent, sSlots, ref sQueryWhereClause); } if (conditionMap != null) { if (conditionMap.Column != null) { BoolExpression conditionExpression = CellCreator.GetConditionExpression(sRootExtent, conditionMap); sQueryWhereClause = BoolExpression.CreateAnd(sQueryWhereClause, conditionExpression); } else { BoolExpression conditionExpression = CellCreator.GetConditionExpression(cNode, conditionMap); cQueryWhereClause = BoolExpression.CreateAnd(cQueryWhereClause, conditionExpression); } } } }
private void WriteAssociationEndMappingElement(EndPropertyMapping endMapping) { this._xmlWriter.WriteStartElement("EndProperty"); this._xmlWriter.WriteAttributeString("Name", endMapping.AssociationEnd.Name); foreach (ScalarPropertyMapping propertyMapping in endMapping.PropertyMappings) { this.WriteScalarPropertyElement(propertyMapping.Property.Name, propertyMapping.Column.Name); } this._xmlWriter.WriteEndElement(); }
private IEnumerable <EdmProperty> GenerateIndependentForeignKeyColumns( EntityType principalEntityType, EntityType dependentEntityType, AssociationSetMapping associationSetMapping, EndPropertyMapping associationEndMapping, EntityType dependentTable, bool isPrimaryKeyColumn, NavigationProperty principalNavigationProperty) { DebugCheck.NotNull(principalEntityType); DebugCheck.NotNull(associationEndMapping); DebugCheck.NotNull(dependentTable); foreach (var property in principalEntityType.KeyProperties()) { var columnName = ((principalNavigationProperty != null) ? principalNavigationProperty.Name : principalEntityType.Name) + "_" + property.Name; var foreignKeyColumn = MapTableColumn(property, columnName, false); dependentTable.AddColumn(foreignKeyColumn); if (isPrimaryKeyColumn) { dependentTable.AddKeyMember(foreignKeyColumn); } foreignKeyColumn.Nullable = associationEndMapping.AssociationEnd.IsOptional() || (associationEndMapping.AssociationEnd.IsRequired() && dependentEntityType.BaseType != null); foreignKeyColumn.StoreGeneratedPattern = StoreGeneratedPattern.None; yield return(foreignKeyColumn); associationEndMapping.AddPropertyMapping(new ScalarPropertyMapping(property, foreignKeyColumn)); if (foreignKeyColumn.Nullable) { associationSetMapping .AddCondition(new IsNullConditionMapping(foreignKeyColumn, false)); } } }
private void GenerateIndependentForeignKeyConstraint( DbDatabaseMapping databaseMapping, EntityType principalEntityType, EntityType dependentEntityType, EntityType dependentTable, AssociationSetMapping associationSetMapping, EndPropertyMapping associationEndMapping, string name, AssociationEndMember principalEnd, bool isPrimaryKeyColumn = false) { DebugCheck.NotNull(databaseMapping); DebugCheck.NotNull(principalEntityType); DebugCheck.NotNull(dependentTable); DebugCheck.NotNull(associationEndMapping); DebugCheck.NotEmpty(name); var principalTable = GetEntityTypeMappingInHierarchy(databaseMapping, principalEntityType) .MappingFragments .Single() .Table; var foreignKeyConstraint = new ForeignKeyBuilder(databaseMapping.Database, name) { PrincipalTable = principalTable, DeleteAction = associationEndMapping.AssociationEnd.DeleteBehavior != OperationAction.None ? associationEndMapping.AssociationEnd.DeleteBehavior : OperationAction.None }; var principalNavigationProperty = databaseMapping.Model.EntityTypes .SelectMany(e => e.DeclaredNavigationProperties) .SingleOrDefault(n => n.ResultEnd == principalEnd); dependentTable.AddForeignKey(foreignKeyConstraint); foreignKeyConstraint.DependentColumns = GenerateIndependentForeignKeyColumns( principalEntityType, dependentEntityType, associationSetMapping, associationEndMapping, dependentTable, isPrimaryKeyColumn, principalNavigationProperty); }
private void WriteAssociationEndMappingElement(EndPropertyMapping endMapping) { DebugCheck.NotNull(endMapping); _xmlWriter.WriteStartElement(MslConstructs.EndPropertyMappingElement); _xmlWriter.WriteAttributeString(MslConstructs.EndPropertyMappingNameAttribute, endMapping.AssociationEnd.Name); foreach (var propertyMapping in endMapping.PropertyMappings) { WriteScalarPropertyElement( propertyMapping.Property.Name, propertyMapping.Column.Name); } _xmlWriter.WriteEndElement(); }
private static EndPropertyMapping GetForeignKeyEndMapFromAssocitionMap( AssociationSetMapping colocatedAssociationSetMap) { MappingFragment mappingFragment = colocatedAssociationSetMap.TypeMappings.First <TypeMapping>().MappingFragments.First <MappingFragment>(); IEnumerable <EdmMember> keyMembers = (IEnumerable <EdmMember>)colocatedAssociationSetMap.StoreEntitySet.ElementType.KeyMembers; foreach (EndPropertyMapping propertyMapping in mappingFragment.PropertyMappings) { EndPropertyMapping endMap = propertyMapping; if (endMap.StoreProperties.SequenceEqual <EdmMember>(keyMembers, (IEqualityComparer <EdmMember>)System.Collections.Generic.EqualityComparer <EdmMember> .Default)) { return(mappingFragment.PropertyMappings.OfType <EndPropertyMapping>().Where <EndPropertyMapping>((Func <EndPropertyMapping, bool>)(eMap => !eMap.Equals((object)endMap))).First <EndPropertyMapping>()); } } return((EndPropertyMapping)null); }
private static void MoveAssociationSetMappingDependents( AssociationSetMapping associationSetMapping, EndPropertyMapping dependentMapping, EntitySet toSet, bool useExistingColumns) { EntityType toTable = toSet.ElementType; dependentMapping.PropertyMappings.Each <ScalarPropertyMapping>((Action <ScalarPropertyMapping>)(pm => { EdmProperty oldColumn = pm.Column; pm.Column = TableOperations.MoveColumnAndAnyConstraints(associationSetMapping.Table, toTable, oldColumn, useExistingColumns); associationSetMapping.Conditions.Where <ConditionPropertyMapping>((Func <ConditionPropertyMapping, bool>)(cc => cc.Column == oldColumn)).Each <ConditionPropertyMapping, EdmProperty>((Func <ConditionPropertyMapping, EdmProperty>)(cc => cc.Column = pm.Column)); })); associationSetMapping.StoreEntitySet = toSet; }
public static void MoveAllDeclaredAssociationSetMappings( DbDatabaseMapping databaseMapping, EntityType entityType, EntityType fromTable, EntityType toTable, bool useExistingColumns) { foreach (AssociationSetMapping associationSetMapping in databaseMapping.EntityContainerMappings.SelectMany <EntityContainerMapping, AssociationSetMapping>((Func <EntityContainerMapping, IEnumerable <AssociationSetMapping> >)(asm => asm.AssociationSetMappings)).Where <AssociationSetMapping>((Func <AssociationSetMapping, bool>)(a => { if (a.Table != fromTable) { return(false); } if (a.AssociationSet.ElementType.SourceEnd.GetEntityType() != entityType) { return(a.AssociationSet.ElementType.TargetEnd.GetEntityType() == entityType); } return(true); })).ToArray <AssociationSetMapping>()) { AssociationEndMember principalEnd; AssociationEndMember dependentEnd; if (!associationSetMapping.AssociationSet.ElementType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd)) { dependentEnd = associationSetMapping.AssociationSet.ElementType.TargetEnd; } if (dependentEnd.GetEntityType() == entityType) { EndPropertyMapping dependentMapping = dependentEnd == associationSetMapping.TargetEndMapping.AssociationEnd ? associationSetMapping.SourceEndMapping : associationSetMapping.TargetEndMapping; AssociationMappingOperations.MoveAssociationSetMappingDependents(associationSetMapping, dependentMapping, databaseMapping.Database.GetEntitySet(toTable), useExistingColumns); (dependentMapping == associationSetMapping.TargetEndMapping ? associationSetMapping.SourceEndMapping : associationSetMapping.TargetEndMapping).PropertyMappings.Each <ScalarPropertyMapping>((Action <ScalarPropertyMapping>)(pm => { if (pm.Column.DeclaringType == toTable) { return; } pm.Column = toTable.Properties.Single <EdmProperty>((Func <EdmProperty, bool>)(p => string.Equals(p.GetPreferredName(), pm.Column.GetPreferredName(), StringComparison.Ordinal))); })); } } }
private void ConfigureAssociationSetMappingForeignKeys(EntitySet entitySet) { foreach (AssociationSetMapping associationSetMapping in this._databaseMapping.EntityContainerMappings.SelectMany <EntityContainerMapping, AssociationSetMapping>((Func <EntityContainerMapping, IEnumerable <AssociationSetMapping> >)(ecm => ecm.AssociationSetMappings)).Where <AssociationSetMapping>((Func <AssociationSetMapping, bool>)(asm => { if (asm.AssociationSet.SourceSet == entitySet || asm.AssociationSet.TargetSet == entitySet) { return(asm.AssociationSet.ElementType.IsRequiredToNonRequired()); } return(false); }))) { AssociationEndMember principalEnd; AssociationEndMember dependentEnd; associationSetMapping.AssociationSet.ElementType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd); if (dependentEnd == associationSetMapping.AssociationSet.ElementType.SourceEnd && associationSetMapping.AssociationSet.SourceSet == entitySet || dependentEnd == associationSetMapping.AssociationSet.ElementType.TargetEnd && associationSetMapping.AssociationSet.TargetSet == entitySet) { EndPropertyMapping endPropertyMapping = associationSetMapping.SourceEndMapping.AssociationEnd == dependentEnd ? associationSetMapping.TargetEndMapping : associationSetMapping.SourceEndMapping; this.MarkColumnsAsNonNullableIfNoTableSharing(entitySet, associationSetMapping.Table, dependentEnd.GetEntityType(), endPropertyMapping.PropertyMappings.Select <ScalarPropertyMapping, EdmProperty>((Func <ScalarPropertyMapping, EdmProperty>)(pm => pm.Column))); } } }
private static bool TryGetWithRelationship( AssociationSetMapping colocatedAssociationSetMap, EntitySetBase thisExtent, MemberPath sRootNode, ref List <SlotInfo> foreignKeySlots, out WithRelationship withRelationship) { withRelationship = (WithRelationship)null; EndPropertyMapping fromAssocitionMap = LeafCellTreeNode.GetForeignKeyEndMapFromAssocitionMap(colocatedAssociationSetMap); if (fromAssocitionMap == null || fromAssocitionMap.AssociationEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many) { return(false); } AssociationEndMember associationEnd = fromAssocitionMap.AssociationEnd; AssociationEndMember otherAssociationEnd = MetadataHelper.GetOtherAssociationEnd(associationEnd); EntityType elementType1 = (EntityType)((RefType)associationEnd.TypeUsage.EdmType).ElementType; EntityType elementType2 = (EntityType)((RefType)otherAssociationEnd.TypeUsage.EdmType).ElementType; AssociationSet set = (AssociationSet)colocatedAssociationSetMap.Set; MemberPath prefix = new MemberPath((EntitySetBase)set, (EdmMember)associationEnd); IEnumerable <ScalarPropertyMapping> source = fromAssocitionMap.PropertyMappings.Cast <ScalarPropertyMapping>(); List <MemberPath> memberPathList = new List <MemberPath>(); foreach (EdmProperty keyMember in elementType1.KeyMembers) { EdmProperty edmProperty = keyMember; ScalarPropertyMapping scalarPropertyMapping = source.Where <ScalarPropertyMapping>((Func <ScalarPropertyMapping, bool>)(propMap => propMap.Property.Equals((object)edmProperty))).First <ScalarPropertyMapping>(); MemberProjectedSlot memberProjectedSlot = new MemberProjectedSlot(new MemberPath(sRootNode, (EdmMember)scalarPropertyMapping.Column)); MemberPath outputMember = new MemberPath(prefix, (EdmMember)edmProperty); memberPathList.Add(outputMember); foreignKeySlots.Add(new SlotInfo(true, true, (ProjectedSlot)memberProjectedSlot, outputMember)); } if (!thisExtent.ElementType.IsAssignableFrom((EdmType)elementType2)) { return(false); } withRelationship = new WithRelationship(set, otherAssociationEnd, elementType2, associationEnd, elementType1, (IEnumerable <MemberPath>)memberPathList); return(true); }
private void GenerateIndependentForeignKeyConstraint( DbDatabaseMapping databaseMapping, EntityType principalEntityType, EntityType dependentEntityType, EntityType dependentTable, AssociationSetMapping associationSetMapping, EndPropertyMapping associationEndMapping, string name, AssociationEndMember principalEnd, bool isPrimaryKeyColumn = false) { EntityType table = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, principalEntityType).MappingFragments.Single <MappingFragment>().Table; ForeignKeyBuilder foreignKeyBuilder = new ForeignKeyBuilder(databaseMapping.Database, name) { PrincipalTable = table, DeleteAction = associationEndMapping.AssociationEnd.DeleteBehavior != OperationAction.None ? associationEndMapping.AssociationEnd.DeleteBehavior : OperationAction.None }; NavigationProperty principalNavigationProperty = databaseMapping.Model.EntityTypes.SelectMany <EntityType, NavigationProperty>((Func <EntityType, IEnumerable <NavigationProperty> >)(e => (IEnumerable <NavigationProperty>)e.DeclaredNavigationProperties)).SingleOrDefault <NavigationProperty>((Func <NavigationProperty, bool>)(n => n.ResultEnd == principalEnd)); dependentTable.AddForeignKey(foreignKeyBuilder); foreignKeyBuilder.DependentColumns = this.GenerateIndependentForeignKeyColumns(principalEntityType, dependentEntityType, associationSetMapping, associationEndMapping, dependentTable, isPrimaryKeyColumn, principalNavigationProperty); }
private static EndPropertyMapping BuildEndPropertyMapping( AssociationSetEnd storeSetEnd, SimpleMappingContext mappingContext) { Debug.Assert(storeSetEnd != null, "storeSetEnd != null"); Debug.Assert(mappingContext != null, "mappingContext != null"); var endPropertyMapping = new EndPropertyMapping { AssociationEnd = mappingContext[storeSetEnd].CorrespondingAssociationEndMember }; foreach (EdmProperty storeKeyMember in storeSetEnd.EntitySet.ElementType.KeyMembers) { var modelKeyMember = mappingContext[storeKeyMember]; var storeFkTableMember = GetAssociatedFkColumn(storeSetEnd, storeKeyMember); endPropertyMapping.AddPropertyMapping( new ScalarPropertyMapping(modelKeyMember, storeFkTableMember)); } return(endPropertyMapping); }