internal AssociationSetEndMappingBuilder(AssociationSetEnd setEnd, StorageEntityType storageEntityType)
        {
            Debug.Assert(setEnd != null, "setEnd should not be null");
            Debug.Assert(storageEntityType != null, "storageEntityType should not be null");

            _setEnd = setEnd;
            _storageEntityType = storageEntityType;
        }
 private CreateEndScalarPropertyCommand(
     AssociationSetMapping associationSetMapping, AssociationSetEnd associationSetEnd, Property entityProperty, Property tableColumn,
     bool enforceConstraints)
 {
     Initialize(entityProperty, tableColumn, enforceConstraints);
     CommandValidation.ValidateAssociationSetMapping(associationSetMapping);
     CommandValidation.ValidateAssociationSetEnd(associationSetEnd);
     _associationSetMapping = associationSetMapping;
     _associationSetEnd = associationSetEnd;
 }
示例#3
0
 internal override bool ParseSingleElement(ICollection <XName> unprocessedElements, XElement elem)
 {
     if (elem.Name.LocalName == AssociationSetEnd.ElementName)
     {
         var ase = new AssociationSetEnd(this, elem);
         _ends.Add(ase);
         ase.Parse(unprocessedElements);
     }
     else
     {
         return(base.ParseSingleElement(unprocessedElements, elem));
     }
     return(true);
 }
 public MappingAssociationSetEnd(EditingContext context, AssociationSetEnd end, MappingEFElement parent)
     : base(context, end, parent)
 {
 }
 internal AssociationSetEndMappingBuilderForViewModel(
     AssociationSetEnd setEnd, StorageEntityType storeEntityType, MappingAssociationSetEnd mase)
     : base(setEnd, storeEntityType)
 {
     _mase = mase;
 }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            Debug.Assert(Association != null, "InvokeInternal is called when Association is null.");
            if (Association == null)
            {
                throw new InvalidOperationException("InvokeInternal is called when Association is null");
            }

            var service = cpc.EditingContext.GetEFArtifactService();
            var artifact = service.Artifact;

            // the entity container we want to add it to
            BaseEntityContainer entityContainer = null;
            switch (CommandModelSpace)
            {
                case ModelSpace.Conceptual:
                    entityContainer = artifact.ConceptualModel().FirstEntityContainer;
                    break;
                case ModelSpace.Storage:
                    entityContainer = artifact.StorageModel().FirstEntityContainer;
                    break;
            }
            Debug.Assert(entityContainer != null, "No entity container");

            // check for uniqueness
            string msg = null;
            if (ModelHelper.IsUniqueName(typeof(AssociationSet), entityContainer, Name, false, out msg) == false)
            {
                throw new InvalidOperationException(msg);
            }

            // create the new item in our model
            var associationSet = new AssociationSet(entityContainer, null);
            associationSet.LocalName.Value = Name;
            entityContainer.AddAssociationSet(associationSet);

            // set the association binding: needs to happen before the ends are resolved
            if (Association != null)
            {
                associationSet.Association.SetRefName(Association);
            }

            // TODO: what should we create if these bindings are unknown?
            var end1 = Association.AssociationEnds()[0];
            var end2 = Association.AssociationEnds()[1];
            if (end1 != null
                && end1.Type.Status == BindingStatus.Known
                && end2 != null
                && end2.Type.Status == BindingStatus.Known)
            {
                var setEnd1 = new AssociationSetEnd(associationSet, null);
                setEnd1.Role.SetRefName(end1);
                setEnd1.EntitySet.SetRefName(end1.Type.Target.EntitySet);
                associationSet.AddAssociationSetEnd(setEnd1);

                var setEnd2 = new AssociationSetEnd(associationSet, null);
                setEnd2.Role.SetRefName(end2);
                setEnd2.EntitySet.SetRefName(end2.Type.Target.EntitySet);
                associationSet.AddAssociationSetEnd(setEnd2);
            }

            XmlModelHelper.NormalizeAndResolve(associationSet);

            _createdAssociationSet = associationSet;
        }
 internal void AddAssociationSetEnd(AssociationSetEnd end)
 {
     _ends.Add(end);
 }
 internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem)
 {
     if (elem.Name.LocalName == AssociationSetEnd.ElementName)
     {
         var ase = new AssociationSetEnd(this, elem);
         _ends.Add(ase);
         ase.Parse(unprocessedElements);
     }
     else
     {
         return base.ParseSingleElement(unprocessedElements, elem);
     }
     return true;
 }
 internal AssociationSetEndMappingBuilderForCommand(AssociationSetEnd setEnd, StorageEntityType storeEntityType)
     : base(setEnd, storeEntityType)
 {
 }
 /// <summary>
 ///     Creates an EndProperty and then creates the ScalarProperty in that End.
 /// </summary>
 /// <param name="associationSetMapping"></param>
 /// <param name="associationSetEnd"></param>
 /// <param name="entityProperty">This must be a valid Property from the C-Model.</param>
 /// <param name="tableColumn">This must be a valid Property from the S-Model.</param>
 internal CreateEndScalarPropertyCommand(
     AssociationSetMapping associationSetMapping, AssociationSetEnd associationSetEnd, Property entityProperty, Property tableColumn)
     : this(associationSetMapping, associationSetEnd, entityProperty, tableColumn, true)
 {
 }
 internal CreateEndPropertyCommand(CreateAssociationSetMappingCommand prereq, AssociationSetEnd associationSetEnd)
     : base(PrereqId)
 {
     AssociationSetEnd = associationSetEnd;
     AddPreReqCommand(prereq);
 }
 internal CreateEndPropertyCommand(AssociationSetMapping associationSetMapping, AssociationSetEnd associationSetEnd)
     : base(PrereqId)
 {
     AssociationSetMapping = associationSetMapping;
     AssociationSetEnd = associationSetEnd;
 }
 internal static void ValidateAssociationSetEnd(AssociationSetEnd setend)
 {
     ValidateEFElement(setend);
 }
示例#14
0
 internal void AddAssociationSetEnd(AssociationSetEnd end)
 {
     _ends.Add(end);
 }
 /// <summary>
 ///     Normalize a ref name relative to an Association End.  This will return true if the symbol is valid & points to a valid
 ///     EFElement, false otherwise.
 /// </summary>
 /// <param name="setEnd"></param>
 /// <param name="parent"></param>
 /// <param name="refName"></param>
 /// <param name="symbol"></param>
 /// <returns></returns>
 internal static NormalizedName NormalizeNameFromAssociationSetEnd(AssociationSetEnd setEnd, EFElement parent, string refName)
 {
     if (setEnd.Role.Status == BindingStatus.Known)
     {
         var end = setEnd.Role.Target;
         if (end.Type.Status == BindingStatus.Known)
         {
             var type = end.Type.Target;
             if (type != null)
             {
                 var cet = type as ConceptualEntityType;
                 if (cet != null)
                 {
                     // thie is a c-side entity type
                     while (cet != null)
                     {
                         var nn = GetNormalizedNameRelativeToEntityType(cet, refName, parent.Artifact.ArtifactSet);
                         if (nn != null)
                         {
                             return nn;
                         }
                         cet = cet.BaseType.Target;
                     }
                 }
                 else
                 {
                     // this is an s-side entity type
                     var nn = GetNormalizedNameRelativeToEntityType(type, refName, parent.Artifact.ArtifactSet);
                     if (nn != null)
                     {
                         return nn;
                     }
                 }
             }
         }
     }
     return null;
 }
        private EndProperty CloneEndProperty(
            CommandProcessorContext cpc,
            EndProperty endToClone, AssociationSetMapping asmInExistingArtifact, AssociationSetEnd aseInExistingArtifact,
            Dictionary<EntityType, EntityType> tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact)
        {
            var createEnd = new CreateEndPropertyCommand(asmInExistingArtifact, aseInExistingArtifact);
            CommandProcessor.InvokeSingleCommand(cpc, createEnd);
            var endInExistingArtifact = createEnd.EndProperty;

            if (null == endInExistingArtifact)
            {
                throw new UpdateModelFromDatabaseException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.UpdateFromDatabaseCannotCreateAssociationSetMappingEndProperty,
                        aseInExistingArtifact.ToPrettyString()));
            }

            var existingArtifact = cpc.Artifact;
            Debug.Assert(existingArtifact != null, "existingArtifact is null for endToClone " + endToClone.ToPrettyString());

            foreach (var sp in endToClone.ScalarProperties())
            {
                if (null == sp.Name.Target)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseScalarPropertyNoNameTarget,
                            sp.ToPrettyString()));
                }
                if (null == sp.ColumnName.Target)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseScalarPropertyNoColumnNameTarget,
                            sp.ToPrettyString()));
                }

                var spCSideEntityTypeinTempArtifact = sp.Name.Target.EntityType as ConceptualEntityType;
                if (null == spCSideEntityTypeinTempArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindEntityTypeForProperty,
                            sp.Name.Target.ToPrettyString()));
                }

                var spSSideEntityTypeinTempArtifact = sp.ColumnName.Target.EntityType as StorageEntityType;
                if (null == spSSideEntityTypeinTempArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindEntityTypeForProperty,
                            sp.ColumnName.Target.ToPrettyString()));
                }

                var csdlEntityTypeInExistingArtifact =
                    FindMatchingConceptualEntityTypeInExistingArtifact(
                        spCSideEntityTypeinTempArtifact,
                        tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact);
                if (null == csdlEntityTypeInExistingArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingEntityType,
                            sp.ToPrettyString(),
                            spCSideEntityTypeinTempArtifact.ToPrettyString()));
                }

                var ssdlEntityTypeInExistingArtifact =
                    FindMatchingStorageEntityTypeInExistingArtifact(spSSideEntityTypeinTempArtifact);
                if (null == ssdlEntityTypeInExistingArtifact)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingEntityType,
                            sp.ToPrettyString(),
                            spSSideEntityTypeinTempArtifact.ToPrettyString()));
                }

                var entityProperty = FindMatchingPropertyInExistingArtifactEntityType(sp.Name.Target, csdlEntityTypeInExistingArtifact);
                if (null == entityProperty)
                {
                    // Cannot find matching property - it must have been unmapped. 
                    // So try to create a new mapped property to which to attach 
                    // this association.

                    // First find S-side Property in temp artifact to which the C-side
                    // Property identified in the AssociationSetMapping is mapped
                    // (Note: cannot use just sp.ColumnName.Target as the S-side Property
                    // used in the AssociationSetMapping can be different from what is used
                    // for the EntitySetMapping in the temp artifact and it is this latter
                    // we need to replicate here).
                    Property sSidePropertyToBeMappedInTempArtifact = null;
                    foreach (var spInTempArtifact in sp.Name.Target.GetAntiDependenciesOfType<ScalarProperty>())
                    {
                        // Ensure that S-side ScalarProperty is from an EntitySetMapping (and not 
                        // an AssociationSetMapping) in the temp artifact.
                        // Can use first one as in temp artifact there is 1:1 mapping.
                        if (null != spInTempArtifact.GetParentOfType(typeof(EntitySetMapping)))
                        {
                            if (null != spInTempArtifact.ColumnName
                                && null != spInTempArtifact.ColumnName.Target)
                            {
                                if (null == sSidePropertyToBeMappedInTempArtifact)
                                {
                                    sSidePropertyToBeMappedInTempArtifact = spInTempArtifact.ColumnName.Target;
                                }
                                else
                                {
                                    // error in temp artifact - there's more than 1 EntitySetMapping ScalarProperty
                                    // mapped to the C-side Property
                                    Debug.Fail(
                                        "C-side Property " + sp.Name.Target.ToPrettyString() +
                                        " has more than 1 ScalarProperty anti-dep with an EntitySetMapping parent. Should be at most 1.");
                                    break;
                                }
                            }
                        }
                    }

                    if (null == sSidePropertyToBeMappedInTempArtifact)
                    {
                        throw new UpdateModelFromDatabaseException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.UpdateFromDatabaseAssociationSetMappingCannotFindSSideForCSideProperty,
                                sp.ToPrettyString(),
                                sp.Name.Target.ToPrettyString()));
                    }

                    // Now find the matching S-side Property in the existing artifact
                    var sSidePropertyToBeMappedInExistingArtifact =
                        FindSSidePropInExistingArtifact(sSidePropertyToBeMappedInTempArtifact);
                    if (null == sSidePropertyToBeMappedInExistingArtifact)
                    {
                        throw new UpdateModelFromDatabaseException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingSSideProperty,
                                sp.ToPrettyString(),
                                sSidePropertyToBeMappedInTempArtifact.ToPrettyString()));
                    }

                    // Now create a new C-side Property in the existing artifact mapped 
                    // to the S-side Property we just found
                    entityProperty = CreateNewConceptualPropertyAndMapping(
                        cpc, sp.Name.Target, sSidePropertyToBeMappedInTempArtifact,
                        csdlEntityTypeInExistingArtifact);
                    if (null == entityProperty)
                    {
                        throw new UpdateModelFromDatabaseException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.UpdateFromDatabaseAssociationSetMappingCannotFindOrCreateMatchingProperty,
                                sp.ToPrettyString(),
                                sp.Name.Target.ToPrettyString(),
                                csdlEntityTypeInExistingArtifact.ToPrettyString()));
                    }
                }

                var tableColumn = FindMatchingPropertyInExistingArtifactEntityType(sp.ColumnName.Target, ssdlEntityTypeInExistingArtifact);
                if (null == tableColumn)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseAssociationSetMappingCannotFindMatchingProperty,
                            sp.ToPrettyString(),
                            sp.ColumnName.Target.ToPrettyString(),
                            ssdlEntityTypeInExistingArtifact.ToPrettyString()));
                }

                var createScalar = new CreateEndScalarPropertyCommand(endInExistingArtifact, entityProperty, tableColumn);
                CommandProcessor.InvokeSingleCommand(cpc, createScalar);
                var existingScalarProp = createScalar.ScalarProperty;
                if (null == existingScalarProp)
                {
                    throw new UpdateModelFromDatabaseException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.UpdateFromDatabaseCannotCreateAssociationSetMappingScalarProperty,
                            entityProperty.ToPrettyString(),
                            tableColumn.ToPrettyString()));
                }
            }

            return endInExistingArtifact;
        }