/// <summary>
        ///     Do the following when an Entity changes:
        ///     - Update roles in related Associations
        /// </summary>
        /// <param name="e"></param>
        public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
        {
            base.ElementPropertyChanged(e);

            var changedNavigationProperty = e.ModelElement as NavigationProperty;

            Debug.Assert(changedNavigationProperty != null);
            Debug.Assert(
                changedNavigationProperty.EntityType != null && changedNavigationProperty.EntityType.EntityDesignerViewModel != null);

            if ((changedNavigationProperty != null) &&
                (changedNavigationProperty.EntityType != null) &&
                (changedNavigationProperty.EntityType.EntityDesignerViewModel != null))
            {
                var tx = ModelUtils.GetCurrentTx(e.ModelElement.Store);
                Debug.Assert(tx != null);
                // don't do the auto update stuff if we are in the middle of deserialization
                if (tx != null &&
                    !tx.IsSerializing)
                {
                    var viewModel = changedNavigationProperty.EntityType.EntityDesignerViewModel;

                    if (e.DomainProperty.Id == NameableItem.NameDomainPropertyId)
                    {
                        // if we are creating this, the old name will be empty so there is no 'change' to do
                        if (String.IsNullOrEmpty((string)e.OldValue))
                        {
                            return;
                        }

                        if (!EscherAttributeContentValidator.IsValidCsdlNavigationPropertyName(changedNavigationProperty.Name))
                        {
                            throw new InvalidOperationException(
                                      String.Format(
                                          CultureInfo.CurrentCulture, Resources.Error_NavigationPropertyNameInvalid,
                                          changedNavigationProperty.Name));
                        }

                        var modelEntityType =
                            viewModel.ModelXRef.GetExisting(changedNavigationProperty.EntityType) as Model.Entity.EntityType;
                        Debug.Assert(modelEntityType != null, "modelEntityType is null");

                        // ensure name is unique
                        if (modelEntityType.LocalName.Value.Equals(changedNavigationProperty.Name, StringComparison.Ordinal))
                        {
                            var msg = string.Format(
                                CultureInfo.CurrentCulture, Model.Resources.Error_MemberNameSameAsParent, changedNavigationProperty.Name,
                                modelEntityType.LocalName.Value);
                            throw new InvalidOperationException(msg);
                        }
                        else if (!EDMModelUtils.IsUniquePropertyName(modelEntityType, changedNavigationProperty.Name, true))
                        {
                            var msg = string.Format(
                                CultureInfo.CurrentCulture, Model.Resources.Error_MemberNameNotUnique, changedNavigationProperty.Name,
                                modelEntityType.LocalName.Value);
                            throw new InvalidOperationException(msg);
                        }

                        ViewModelChangeContext.GetNewOrExistingContext(tx)
                        .ViewModelChanges.Add(new NavigationPropertyChange(changedNavigationProperty));
                    }
                }
            }
        }
Esempio n. 2
0
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (_needsValidation)
            {
                _needsValidation = false;
                string msg = null;

                if (!EscherAttributeContentValidator.IsValidCsdlAssociationName(AssociationName))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.NewAssociationDialog_InvalidAssociationNameMsg);
                    e.Cancel = true;
                    associationNameTextBox.Focus();
                }
                else if (navigationPropertyCheckbox.Checked &&
                         !EscherAttributeContentValidator.IsValidCsdlNavigationPropertyName(End1NavigationPropertyName))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.NewAssociationDialog_InvalidNavigationPropertyNameMsg);
                    e.Cancel = true;
                    navigationProperty1TextBox.Focus();
                }
                else if (navigationProperty2Checkbox.Checked &&
                         !EscherAttributeContentValidator.IsValidCsdlNavigationPropertyName(End2NavigationPropertyName))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.NewAssociationDialog_InvalidNavigationPropertyNameMsg);
                    e.Cancel = true;
                    navigationProperty2TextBox.Focus();
                }
                else if (!ModelHelper.IsUniqueName(typeof(Association), End1Entity.Parent, AssociationName, false, out msg))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.NewAssociationDialog_EnsureUniqueNameMsg);
                    e.Cancel = true;
                    associationNameTextBox.Focus();
                }
                else if (navigationPropertyCheckbox.Checked &&
                         End1NavigationPropertyName.Equals(End1Entity.LocalName.Value, StringComparison.Ordinal))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.SameEntityAndPropertyNameMsg);
                    e.Cancel = true;
                    navigationProperty1TextBox.Focus();
                }
                else if (navigationPropertyCheckbox.Checked &&
                         !ModelHelper.IsUniquePropertyName(End1Entity, End1NavigationPropertyName, true))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.NewAssociationDialog_EnsureUniquePropertyNameMsg);
                    e.Cancel = true;
                    navigationProperty1TextBox.Focus();
                }
                else if (End2NavigationPropertyName.Equals(End2Entity.LocalName.Value, StringComparison.Ordinal))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.SameEntityAndPropertyNameMsg);
                    e.Cancel = true;
                    navigationProperty2TextBox.Focus();
                }
                else if (navigationProperty2Checkbox.Checked &&
                         !ModelHelper.IsUniquePropertyName(End2Entity, End2NavigationPropertyName, true) ||
                         (End1Entity == End2Entity && navigationProperty2Checkbox.Checked &&
                          End2NavigationPropertyName.Equals(End1NavigationPropertyName, StringComparison.Ordinal)))
                {
                    VsUtils.ShowErrorDialog(DialogsResource.NewAssociationDialog_EnsureUniquePropertyNameMsg);
                    e.Cancel = true;
                    navigationProperty2TextBox.Focus();
                }
            }
        }