// finds the list of C-side objects mapped through MappingFragments or AssociationSetMappings // to the passed in StorageEntitySet private static HashSet <EFObject> FindMappedConceptualObjects(StorageEntitySet storageEntitySet) { var mappedConceptualObjects = new HashSet <EFObject>(); if (null == storageEntitySet) { Debug.Fail("storageEntitySet must not be null"); return(mappedConceptualObjects); } // for each MappingFragment find the EntityTypeMapping parent and // add each EntityType referenced. Also add any EntitySet which has an // anti-dependency on that EntityType. foreach (var mf in storageEntitySet.GetAntiDependenciesOfType <MappingFragment>()) { var etm = mf.EntityTypeMapping; if (null != etm && null != etm.TypeName && null != etm.TypeName.Bindings) { foreach (var binding in etm.TypeName.Bindings) { var et = binding.Target as ConceptualEntityType; if (null != et) { mappedConceptualObjects.Add(et); foreach (var es in et.GetAntiDependenciesOfType <ConceptualEntitySet>()) { mappedConceptualObjects.Add(es); } } } } } // for each AssociationSetMapping add any referenced C-side AssociationSet and // also any referenced C-side Association foreach (var asm in storageEntitySet.GetAntiDependenciesOfType <AssociationSetMapping>()) { if (null != asm.Name && null != asm.Name.Target) { var assocSet = asm.Name.Target; mappedConceptualObjects.Add(assocSet); if (null != assocSet.Association && null != assocSet.Association.Target) { mappedConceptualObjects.Add(assocSet.Association.Target); } } if (null != asm.TypeName && null != asm.TypeName.Target) { mappedConceptualObjects.Add(asm.TypeName.Target); } } return(mappedConceptualObjects); }
internal static DatabaseObject CreateFromEntitySet(StorageEntitySet ses) { var dbObj = new DatabaseObject(); dbObj.Schema = ses.DatabaseSchemaName; dbObj.Name = ses.DatabaseTableName; return(dbObj); }
/// <summary> /// Creates a MappingFragment for the passed in StorageEntitySet in the passed in ETM. /// </summary> /// <param name="entityTypeMapping">This must a valid EntityTypeMapping.</param> /// <param name="entitySet">This must be a valid StorageEntitySet.</param> internal CreateMappingFragmentCommand(EntityTypeMapping entityTypeMapping, StorageEntitySet storageEntitySet) { CommandValidation.ValidateEntityTypeMapping(entityTypeMapping); CommandValidation.ValidateStorageEntitySet(storageEntitySet); ConceptualEntityType = entityTypeMapping.FirstBoundConceptualEntityType; _entityTypeMappingKind = entityTypeMapping.Kind; _entityTypeMapping = entityTypeMapping; StorageEntitySet = storageEntitySet; }
internal CreateAssociationSetMappingCommand(Association association, EntityType storageEntityType) : base(PrereqId) { CommandValidation.ValidateAssociation(association); CommandValidation.ValidateStorageEntityType(storageEntityType); EntityContainerMapping = association.Artifact.MappingModel().FirstEntityContainerMapping; AssociationSet = association.AssociationSet; Association = association; StorageEntitySet = storageEntityType.EntitySet as StorageEntitySet; }
/// <summary> /// Create MappingFragment for the passed in ConceptualEntityType and StoreEntityType, and inside the /// ETM based on the passed in kind. /// </summary> /// <param name="conceptualEntityType">This must be a valid EntityType from the C-Model.</param> /// <param name="storeEntityType">This must be a valid EntityType from the S-Model.</param> /// <param name="kind">Specify whether this should be put in an IsTypeOf or Default ETM</param> internal CreateMappingFragmentCommand(EntityType conceptualEntityType, EntityType storeEntityType, EntityTypeMappingKind kind) { CommandValidation.ValidateStorageEntityType(storeEntityType); CommandValidation.ValidateConceptualEntityType(conceptualEntityType); ConceptualEntityType = conceptualEntityType; _entityTypeMappingKind = kind; _entityTypeMapping = null; StorageEntitySet = storeEntityType.EntitySet as StorageEntitySet; CommandValidation.ValidateStorageEntitySet(StorageEntitySet); }
internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem) { if (elem.Name.LocalName == EntitySet.ElementName) { EntitySet es = new StorageEntitySet(this, elem); AddEntitySet(es); es.Parse(unprocessedElements); } else { return base.ParseSingleElement(unprocessedElements, elem); } return true; }
internal CreateAssociationSetMappingCommand( EntityContainerMapping entityContainerMapping, AssociationSet associationSet, Association association, StorageEntitySet storageEntitySet) : base(PrereqId) { CommandValidation.ValidateEntityContainerMapping(entityContainerMapping); CommandValidation.ValidateAssociationSet(associationSet); CommandValidation.ValidateAssociation(association); CommandValidation.ValidateStorageEntitySet(storageEntitySet); EntityContainerMapping = entityContainerMapping; AssociationSet = associationSet; Association = association; StorageEntitySet = storageEntitySet; }
/// <summary> /// Creates a new MappingFragment in the existing EntityTypeMapping /// based on another MappingFragment (fragToClone) in a different artifact. /// All the other parameters are presumed to already exist in the same artifact /// as the EntityTypeMapping. /// </summary> internal static MappingFragment CloneMappingFragment( CommandProcessorContext cpc, MappingFragment fragToClone, EntityTypeMapping existingEntityTypeMapping, StorageEntitySet existingEntitySet) { var createFragmentCommand = new CreateMappingFragmentCommand(existingEntityTypeMapping, existingEntitySet); var cp = new CommandProcessor(cpc, createFragmentCommand); cp.Invoke(); var frag = createFragmentCommand.MappingFragment; Debug.Assert(frag != null, "Could not locate or create the required mapping fragment"); if (frag != null) { foreach (var sp in fragToClone.ScalarProperties()) { Property entityProperty = null; if (sp.Name != null && sp.Name.Target != null && sp.Name.Target.LocalName != null && sp.Name.Target.LocalName.Value != null) { entityProperty = ModelHelper.FindPropertyForEntityTypeMapping( existingEntityTypeMapping, sp.Name.Target.LocalName.Value); Debug.Assert( entityProperty != null, "Cannot find Property with name " + sp.Name.Target.LocalName.Value + " for EntityTypeMapping " + existingEntityTypeMapping.ToPrettyString()); } Property tableColumn = null; if (frag.StoreEntitySet != null && frag.StoreEntitySet.Target != null && frag.StoreEntitySet.Target.EntityType != null && frag.StoreEntitySet.Target.EntityType.Target != null && sp.ColumnName != null && sp.ColumnName.Target != null && sp.ColumnName.Target.LocalName != null && sp.ColumnName.Target.LocalName.Value != null) { tableColumn = ModelHelper.FindProperty( frag.StoreEntitySet.Target.EntityType.Target, sp.ColumnName.Target.LocalName.Value); Debug.Assert( tableColumn != null, "Cannot find Property with name " + sp.ColumnName.Target.LocalName.Value + " for EntityType " + frag.StoreEntitySet.Target.EntityType.Target.ToPrettyString()); } if (entityProperty != null && tableColumn != null) { var createScalarCommand = new CreateFragmentScalarPropertyCommand(frag, entityProperty, tableColumn); var cp2 = new CommandProcessor(cpc, createScalarCommand); cp2.Invoke(); } } } return(frag); }
protected override void InvokeInternal(CommandProcessorContext cpc) { Debug.Assert(EntityType != null, "InvokeInternal is called when EntityType is null"); if (EntityType == null) { throw new InvalidOperationException("InvokeInternal is called when EntityType is null"); } var service = cpc.EditingContext.GetEFArtifactService(); var artifact = service.Artifact; // locate the entity container we want to add it to BaseEntityContainer entityContainer = null; EntitySet entitySet = null; switch (ModelSpaceValue) { case ModelSpace.Conceptual: Debug.Assert(artifact.ConceptualModel() != null, "artifact.ConceptualModel() should not be null"); entityContainer = artifact.ConceptualModel().FirstEntityContainer; break; case ModelSpace.Storage: Debug.Assert(artifact.StorageModel() != null, "artifact.StorageModel() should not be null"); entityContainer = artifact.StorageModel().FirstEntityContainer; break; default: Debug.Fail("Unknown model space"); break; } Debug.Assert(entityContainer != null, "entityContainer should not be null"); if (entityContainer == null) { throw new CannotLocateParentItemException(); } // check for uniqueness of the name within the chosen EC if (UniquifyName) { Name = ModelHelper.GetUniqueName(typeof(EntitySet), entityContainer, Name); } else { string msg = null; if (ModelHelper.IsUniqueName(typeof(EntitySet), entityContainer, Name, false, out msg) == false) { throw new CommandValidationFailedException(msg); } } // create the entity set; don't need to assert on enum since that has happened above if (ModelSpaceValue == ModelSpace.Conceptual) { entitySet = new ConceptualEntitySet(entityContainer, null); } else { entitySet = new StorageEntitySet(entityContainer, null); // DefiningQuery creation if (DefiningQueryContent != null) { var definingQuery = new DefiningQuery(entitySet, null); definingQuery.XElement.SetValue(DefiningQueryContent); ((StorageEntitySet)entitySet).DefiningQuery = definingQuery; } } Debug.Assert(entitySet != null, "entitySet should not be null"); if (entitySet == null) { throw new ItemCreationFailureException(); } // set name and add to the parent entitySet.LocalName.Value = Name; entityContainer.AddEntitySet(entitySet); // set the entity type binding if (EntityType != null) { entitySet.EntityType.SetRefName(EntityType); } XmlModelHelper.NormalizeAndResolve(entitySet); EntitySet = entitySet; }
internal static void ValidateStorageEntitySet(StorageEntitySet storageEntitySet) { ValidateEFElement(storageEntitySet); Debug.Assert(storageEntitySet.EntityType.Target != null, "The passed in S-Side EntitySet has unknown entity binding"); }
/// <summary> /// Creates a new MappingFragment in the existing EntityTypeMapping /// based on another MappingFragment (fragToClone) in a different artifact. /// All the other parameters are presumed to already exist in the same artifact /// as the EntityTypeMapping. /// </summary> internal static MappingFragment CloneMappingFragment( CommandProcessorContext cpc, MappingFragment fragToClone, EntityTypeMapping existingEntityTypeMapping, StorageEntitySet existingEntitySet) { var createFragmentCommand = new CreateMappingFragmentCommand(existingEntityTypeMapping, existingEntitySet); var cp = new CommandProcessor(cpc, createFragmentCommand); cp.Invoke(); var frag = createFragmentCommand.MappingFragment; Debug.Assert(frag != null, "Could not locate or create the required mapping fragment"); if (frag != null) { foreach (var sp in fragToClone.ScalarProperties()) { Property entityProperty = null; if (sp.Name != null && sp.Name.Target != null && sp.Name.Target.LocalName != null && sp.Name.Target.LocalName.Value != null) { entityProperty = ModelHelper.FindPropertyForEntityTypeMapping( existingEntityTypeMapping, sp.Name.Target.LocalName.Value); Debug.Assert( entityProperty != null, "Cannot find Property with name " + sp.Name.Target.LocalName.Value + " for EntityTypeMapping " + existingEntityTypeMapping.ToPrettyString()); } Property tableColumn = null; if (frag.StoreEntitySet != null && frag.StoreEntitySet.Target != null && frag.StoreEntitySet.Target.EntityType != null && frag.StoreEntitySet.Target.EntityType.Target != null && sp.ColumnName != null && sp.ColumnName.Target != null && sp.ColumnName.Target.LocalName != null && sp.ColumnName.Target.LocalName.Value != null) { tableColumn = ModelHelper.FindProperty( frag.StoreEntitySet.Target.EntityType.Target, sp.ColumnName.Target.LocalName.Value); Debug.Assert( tableColumn != null, "Cannot find Property with name " + sp.ColumnName.Target.LocalName.Value + " for EntityType " + frag.StoreEntitySet.Target.EntityType.Target.ToPrettyString()); } if (entityProperty != null && tableColumn != null) { var createScalarCommand = new CreateFragmentScalarPropertyCommand(frag, entityProperty, tableColumn); var cp2 = new CommandProcessor(cpc, createScalarCommand); cp2.Invoke(); } } } return frag; }
// finds the list of C-side objects mapped through MappingFragments or AssociationSetMappings // to the passed in StorageEntitySet private static HashSet<EFObject> FindMappedConceptualObjects(StorageEntitySet storageEntitySet) { var mappedConceptualObjects = new HashSet<EFObject>(); if (null == storageEntitySet) { Debug.Fail("storageEntitySet must not be null"); return mappedConceptualObjects; } // for each MappingFragment find the EntityTypeMapping parent and // add each EntityType referenced. Also add any EntitySet which has an // anti-dependency on that EntityType. foreach (var mf in storageEntitySet.GetAntiDependenciesOfType<MappingFragment>()) { var etm = mf.EntityTypeMapping; if (null != etm && null != etm.TypeName && null != etm.TypeName.Bindings) { foreach (var binding in etm.TypeName.Bindings) { var et = binding.Target as ConceptualEntityType; if (null != et) { mappedConceptualObjects.Add(et); foreach (var es in et.GetAntiDependenciesOfType<ConceptualEntitySet>()) { mappedConceptualObjects.Add(es); } } } } } // for each AssociationSetMapping add any referenced C-side AssociationSet and // also any referenced C-side Association foreach (var asm in storageEntitySet.GetAntiDependenciesOfType<AssociationSetMapping>()) { if (null != asm.Name && null != asm.Name.Target) { var assocSet = asm.Name.Target; mappedConceptualObjects.Add(assocSet); if (null != assocSet.Association && null != assocSet.Association.Target) { mappedConceptualObjects.Add(assocSet.Association.Target); } } if (null != asm.TypeName && null != asm.TypeName.Target) { mappedConceptualObjects.Add(asm.TypeName.Target); } } return mappedConceptualObjects; }
/// <summary> /// Creates a new AssociationSetMapping in the existing EntityContainerMapping /// based on another AssociationSetMapping (asmToClone) in a different artifact. /// All the other parameters are presumed to already exist in the same artifact /// as the EntityContainerMapping. /// </summary> private AssociationSetMapping CloneAssociationSetMapping( CommandProcessorContext cpc, AssociationSetMapping asmToClone, EntityContainerMapping existingEntityContainerMapping, AssociationSet existingAssociationSet, Association existingAssociation, StorageEntitySet existingStorageEntitySet, Dictionary<EntityType, EntityType> tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact) { var createASM = new CreateAssociationSetMappingCommand( existingEntityContainerMapping, existingAssociationSet, existingAssociation, existingStorageEntitySet); CommandProcessor.InvokeSingleCommand(cpc, createASM); var asmInExistingArtifact = createASM.AssociationSetMapping; if (null == asmInExistingArtifact) { throw new UpdateModelFromDatabaseException( string.Format( CultureInfo.CurrentCulture, Resources.UpdateFromDatabaseCannotCreateAssociationSetMapping, existingAssociationSet.ToPrettyString())); } // cannot just look for an AssociationSetEnd with the same Role name in // the existing artifact as the role may have changed when the Association was // copied into the existing artifact - but we do know the ends were created in // the same order - so simply match them up var existingAssocSetEnds = existingAssociationSet.AssociationSetEnds().ToArray(); if (2 != existingAssocSetEnds.Length) { throw new UpdateModelFromDatabaseException( string.Format( CultureInfo.CurrentCulture, Resources.UpdateFromDatabaseAssociationSetMappingWrongNumberAssociationSetEnds, existingAssociationSet.ToPrettyString(), existingAssocSetEnds.Length)); } var endsToClone = asmToClone.EndProperties().ToArray(); if (2 != endsToClone.Length) { throw new UpdateModelFromDatabaseException( string.Format( CultureInfo.CurrentCulture, Resources.UpdateFromDatabaseAssociationSetMappingWrongNumberAssociationSetEnds, existingAssociationSet.ToPrettyString(), existingAssocSetEnds.Length)); } for (var i = 0; i < 2; i++) { var aseInExistingArtifact = existingAssocSetEnds[i]; var endToClone = endsToClone[i]; CloneEndProperty( cpc, endToClone, asmInExistingArtifact, aseInExistingArtifact, tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact); } return asmInExistingArtifact; }