Пример #1
0
        public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
        {
            base.ElementPropertyChanged(e);

            Association element   = (Association)e.ModelElement;
            Store       store     = element.Store;
            Transaction current   = store.TransactionManager.CurrentTransaction;
            ModelRoot   modelRoot = store.ElementDirectory.FindElements <ModelRoot>().FirstOrDefault();

            if (current.IsSerializing)
            {
                return;
            }

            List <string> errorMessages = EFCoreValidator.GetErrors(element).ToList();

            switch (e.DomainProperty.Name)
            {
            case "Persistent":
                UpdateDisplayForPersistence(element);
                break;

            case "TargetPropertyName":
                errorMessages.Add(ValidateAssociationIdentifier(element, element.Source, element.Target, (string)e.NewValue));
                break;

            case "SourcePropertyName":
                errorMessages.Add(ValidateAssociationIdentifier(element, element.Target, element.Source, (string)e.NewValue));
                break;

            case "SourceMultiplicity":
                Multiplicity newSourceMultiplicity = (Multiplicity)e.NewValue;

                if ((newSourceMultiplicity == Multiplicity.One && element.TargetMultiplicity == Multiplicity.One) ||
                    (newSourceMultiplicity == Multiplicity.ZeroOne && element.TargetMultiplicity == Multiplicity.ZeroOne))
                {
                    element.SourceRole = EndpointRole.NotSet;
                    element.TargetRole = EndpointRole.NotSet;
                }
                else
                {
                    SetEndpointRoles(element);
                }

                UpdateDisplayForCascadeDelete(element, null, null, newSourceMultiplicity);
                break;

            case "TargetMultiplicity":
                Multiplicity newTargetMultiplicity = (Multiplicity)e.NewValue;

                if ((element.SourceMultiplicity == Multiplicity.One && newTargetMultiplicity == Multiplicity.One) ||
                    (element.SourceMultiplicity == Multiplicity.ZeroOne && newTargetMultiplicity == Multiplicity.ZeroOne))
                {
                    element.SourceRole = EndpointRole.NotSet;
                    element.TargetRole = EndpointRole.NotSet;
                }
                else
                {
                    SetEndpointRoles(element);
                }

                UpdateDisplayForCascadeDelete(element, null, null, null, newTargetMultiplicity);
                break;

            case "SourceRole":
                EndpointRole newSourceRole = (EndpointRole)e.NewValue;
                if (element.TargetRole == EndpointRole.NotSet && newSourceRole == EndpointRole.Dependent)
                {
                    element.TargetRole = EndpointRole.Principal;
                }
                else if (element.TargetRole == EndpointRole.NotSet && newSourceRole == EndpointRole.Principal)
                {
                    element.TargetRole = EndpointRole.Dependent;
                }

                break;

            case "TargetRole":
                EndpointRole newTargetRole = (EndpointRole)e.NewValue;
                if (element.SourceRole == EndpointRole.NotSet && newTargetRole == EndpointRole.Dependent)
                {
                    element.SourceRole = EndpointRole.Principal;
                }
                else if (element.SourceRole == EndpointRole.NotSet && newTargetRole == EndpointRole.Principal)
                {
                    element.SourceRole = EndpointRole.Dependent;
                }

                break;

            case "SourceDeleteAction":
                DeleteAction sourceDeleteAction = (DeleteAction)e.NewValue;
                UpdateDisplayForCascadeDelete(element, sourceDeleteAction);

                break;

            case "TargetDeleteAction":
                DeleteAction targetDeleteAction = (DeleteAction)e.NewValue;
                UpdateDisplayForCascadeDelete(element, null, targetDeleteAction);
                break;
            }

            errorMessages = errorMessages.Where(m => m != null).ToList();
            if (errorMessages.Any())
            {
                current.Rollback();
                MessageBox.Show(string.Join("; ", errorMessages));
            }
        }
        /// <summary>
        ///    Returns the property descriptors for the described Association domain class, adding tracking property
        ///    descriptor(s).
        /// </summary>
        private PropertyDescriptorCollection GetCustomProperties(Attribute[] attributes)
        {
            // Get the default property descriptors from the base class
            PropertyDescriptorCollection propertyDescriptors = base.GetProperties(attributes);

            //Add the descriptor for the tracking property.
            if (ModelElement is Association association)
            {
                ModelRoot modelRoot = association.Source.ModelRoot;
                storeDomainDataDirectory = association.Store.DomainDataDirectory;
                BidirectionalAssociation bidirectionalAssociation = association as BidirectionalAssociation;

                // show FKPropertyName only when possible and required
                if (!modelRoot.ExposeForeignKeys ||
                    (association.SourceRole != EndpointRole.Dependent && association.TargetRole != EndpointRole.Dependent))
                {
                    propertyDescriptors.Remove("FKPropertyName");
                }

                // EF6 can't have declared foreign keys for 1..1 / 0-1..1 / 1..0-1 / 0-1..0-1 relationships
                if (modelRoot.EntityFrameworkVersion == EFVersion.EF6 &&
                    association.SourceMultiplicity != Multiplicity.ZeroMany &&
                    association.TargetMultiplicity != Multiplicity.ZeroMany)
                {
                    propertyDescriptors.Remove("FKPropertyName");
                }

                // no FKs for aggregates
                if (association.Source.IsDependentType || association.Target.IsDependentType)
                {
                    propertyDescriptors.Remove("FKPropertyName");
                }

                // only display roles for 1..1 and 0-1..0-1 associations
                if ((association.SourceMultiplicity != Multiplicity.One || association.TargetMultiplicity != Multiplicity.One) &&
                    (association.SourceMultiplicity != Multiplicity.ZeroOne || association.TargetMultiplicity != Multiplicity.ZeroOne))
                {
                    propertyDescriptors.Remove("SourceRole");
                    propertyDescriptors.Remove("TargetRole");
                }

                // only display delete behavior on the principal end
                // except that owned types don't have deletiion behavior choices
                if (association.SourceRole != EndpointRole.Principal || association.Source.IsDependentType || association.Target.IsDependentType)
                {
                    propertyDescriptors.Remove("SourceDeleteAction");
                }

                if (association.TargetRole != EndpointRole.Principal || association.Source.IsDependentType || association.Target.IsDependentType)
                {
                    propertyDescriptors.Remove("TargetDeleteAction");
                }

                // only show JoinTableName if is *..* association
                if (association.SourceMultiplicity != Multiplicity.ZeroMany || association.TargetMultiplicity != Multiplicity.ZeroMany)
                {
                    propertyDescriptors.Remove("JoinTableName");
                }

                // implementNotify implicitly defines autoproperty as false, so we don't display it
                if (association.TargetImplementNotify)
                {
                    propertyDescriptors.Remove("TargetAutoProperty");
                }
                if (bidirectionalAssociation != null && bidirectionalAssociation.SourceImplementNotify)
                {
                    propertyDescriptors.Remove("SourceAutoProperty");
                }

                // we're only allowing ..1 and ..0-1 associations to have backing fields
                if (association.TargetMultiplicity == Multiplicity.ZeroMany)
                {
                    propertyDescriptors.Remove("TargetAutoProperty");
                }
                if (bidirectionalAssociation != null && bidirectionalAssociation.SourceMultiplicity == Multiplicity.ZeroMany)
                {
                    propertyDescriptors.Remove("SourceAutoProperty");
                }

                // EF6 doesn't support property access modes
                if (modelRoot.EntityFrameworkVersion == EFVersion.EF6)
                {
                    propertyDescriptors.Remove("TargetPropertyAccessMode");
                    propertyDescriptors.Remove("SourcePropertyAccessMode");
                }

                // only show backing field name and property access mode if not an autoproperty
                if (association.TargetAutoProperty)
                {
                    propertyDescriptors.Remove("TargetBackingFieldName");
                    propertyDescriptors.Remove("TargetPropertyAccessMode");
                }

                if (bidirectionalAssociation == null || bidirectionalAssociation.SourceAutoProperty)
                {
                    propertyDescriptors.Remove("SourceBackingFieldName");
                    propertyDescriptors.Remove("SourcePropertyAccessMode");
                }

                /********************************************************************************/

                //Add the descriptors for the tracking properties

                propertyDescriptors.Add(new TrackingPropertyDescriptor(association
                                                                       , storeDomainDataDirectory.GetDomainProperty(Association.CollectionClassDomainPropertyId)
                                                                       , storeDomainDataDirectory.GetDomainProperty(Association.IsCollectionClassTrackingDomainPropertyId)
                                                                       , new Attribute[]
                {
                    new DisplayNameAttribute("Collection Class")
                    , new DescriptionAttribute("Type of collections generated. Overrides the default collection class for the model")
                    , new CategoryAttribute("Code Generation")
                }));

                if (association.TargetMultiplicity == Multiplicity.One || association.TargetMultiplicity == Multiplicity.ZeroOne)
                {
                    propertyDescriptors.Add(new TrackingPropertyDescriptor(association
                                                                           , storeDomainDataDirectory.GetDomainProperty(Association.TargetImplementNotifyDomainPropertyId)
                                                                           , storeDomainDataDirectory.GetDomainProperty(Association.IsTargetImplementNotifyTrackingDomainPropertyId)
                                                                           , new Attribute[]
                    {
                        new DisplayNameAttribute("Implement INotifyPropertyChanged")
                        , new DescriptionAttribute("Should this end participate in INotifyPropertyChanged activities? "
                                                   + "Only valid for non-collection targets.")
                        , new CategoryAttribute("End 2")
                    }));

                    propertyDescriptors.Add(new TrackingPropertyDescriptor(association
                                                                           , storeDomainDataDirectory.GetDomainProperty(Association.TargetAutoPropertyDomainPropertyId)
                                                                           , storeDomainDataDirectory.GetDomainProperty(Association.IsTargetAutoPropertyTrackingDomainPropertyId)
                                                                           , new Attribute[]
                    {
                        new DisplayNameAttribute("End1 Is Auto Property")
                        , new DescriptionAttribute("If false, generates a backing field and a partial method to hook getting and setting the property. "
                                                   + "If true, generates a simple auto property. Only valid for non-collection properties.")
                        , new CategoryAttribute("End 2")
                    }));
                }

                if (bidirectionalAssociation?.SourceMultiplicity == Multiplicity.One || bidirectionalAssociation?.SourceMultiplicity == Multiplicity.ZeroOne)
                {
                    propertyDescriptors.Add(new TrackingPropertyDescriptor(bidirectionalAssociation
                                                                           , storeDomainDataDirectory.GetDomainProperty(BidirectionalAssociation.SourceImplementNotifyDomainPropertyId)
                                                                           , storeDomainDataDirectory.GetDomainProperty(BidirectionalAssociation.IsSourceImplementNotifyTrackingDomainPropertyId)
                                                                           , new Attribute[]
                    {
                        new DisplayNameAttribute("Implement INotifyPropertyChanged")
                        , new DescriptionAttribute("Should this end participate in INotifyPropertyChanged activities? "
                                                   + "Only valid for non-collection targets.")
                        , new CategoryAttribute("End 1")
                    }));

                    propertyDescriptors.Add(new TrackingPropertyDescriptor(association
                                                                           , storeDomainDataDirectory.GetDomainProperty(BidirectionalAssociation.SourceAutoPropertyDomainPropertyId)
                                                                           , storeDomainDataDirectory.GetDomainProperty(BidirectionalAssociation.IsSourceAutoPropertyTrackingDomainPropertyId)
                                                                           , new Attribute[]
                    {
                        new DisplayNameAttribute("End2 Is Auto Property")
                        , new DescriptionAttribute("If false, generates a backing field and a partial method to hook getting and setting the property. "
                                                   + "If true, generates a simple auto property. Only valid for non-collection properties.")
                        , new CategoryAttribute("End 1")
                    }));
                }
            }

            // Return the property descriptors for this element
            return(propertyDescriptors);
        }
Пример #3
0
        private static void DoCustomLayout(List <NodeShape> nodeShapes, List <BinaryLinkShape> linkShapes, ModelRoot modelRoot)
        {
            GeometryGraph graph = new GeometryGraph();

            CreateDiagramNodes(nodeShapes, graph);
            CreateDiagramLinks(linkShapes, graph);

            AddDesignConstraints(linkShapes, modelRoot, graph);

            LayoutHelpers.CalculateLayout(graph, modelRoot.LayoutAlgorithmSettings, null);

            // Move model to positive axis.
            graph.UpdateBoundingBox();
            graph.Translate(new Point(-graph.Left, -graph.Bottom));

            UpdateNodePositions(graph);
            UpdateConnectors(graph);
        }
Пример #4
0
        private void SetInitialMultiplicity(UnidirectionalAssociation element)
        {
            // valid unidirectional associations:
            // EF6 - entity to entity, entity to dependent
            // EFCore - entity to entity, entity to dependent
            // EFCore5Plus - entity to entity, entity to dependent, dependent to dependent, keyless to entity

            ModelRoot modelRoot = element.Source.ModelRoot;
            EFVersion entityFrameworkVersion = modelRoot.EntityFrameworkVersion;

            if (entityFrameworkVersion == EFVersion.EF6)
            {
                if (element.Source.IsEntity() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroMany;
                }

                if (element.Source.IsEntity() && element.Target.IsDependent())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.One;
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && !modelRoot.IsEFCore5Plus)
            {
                if (element.Source.IsEntity() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroMany;
                }

                if (element.Source.IsEntity() && element.Target.IsDependent())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroOne;
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && modelRoot.IsEFCore5Plus)
            {
                if (element.Source.IsEntity() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroMany;
                }

                if (element.Source.IsEntity() && element.Target.IsDependent())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroOne;
                }

                if (element.Source.IsDependent() && element.Target.IsDependent())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroOne;
                }

                if (element.Source.IsKeyless() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.ZeroMany;
                    element.TargetMultiplicity = Multiplicity.One;
                }
            }
        }
        public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
        {
            base.ElementPropertyChanged(e);

            ModelAttribute element    = (ModelAttribute)e.ModelElement;
            ModelClass     modelClass = element.ModelClass;
            ModelRoot      modelRoot  = modelClass.ModelRoot;

            Store       store   = element.Store;
            Transaction current = store.TransactionManager.CurrentTransaction;

            if (current.IsSerializing)
            {
                return;
            }

            if (Equals(e.NewValue, e.OldValue))
            {
                return;
            }

            List <string> errorMessages = EFCoreValidator.GetErrors(element).ToList();

            switch (e.DomainProperty.Name)
            {
            case "AutoProperty":

                if (element.AutoProperty && modelClass.ImplementNotify)
                {
                    WarningDisplay.Show($"{modelClass.Name}.{element.Name} is an autoproperty, so will not participate in INotifyPropertyChanged messages");
                }

                break;

            case "Indexed":

                if (element.IsIdentity)
                {
                    element.Indexed = true;
                }

                if (element.IsConcurrencyToken)
                {
                    element.Indexed = false;
                }

                if (element.Indexed)
                {
                    element.Persistent = true;
                }

                break;

            case "Type":
                string newType = (string)e.NewValue;

                if (element.IsIdentity)
                {
                    if (!modelRoot.ValidIdentityAttributeTypes.Contains(ModelAttribute.ToCLRType(newType)))
                    {
                        errorMessages.Add($"{modelClass.Name}.{element.Name}: Properties of type {newType} can't be used as identity properties.");
                    }
                    else
                    {
                        element.Required   = true;
                        element.Persistent = true;
                    }
                }

                if (newType != "String")
                {
                    element.MaxLength  = 0;
                    element.StringType = HTML5Type.None;
                }
                else
                {
                    if (!element.IsValidInitialValue(newType))
                    {
                        element.InitialValue = null;
                    }
                }

                if (element.IsConcurrencyToken)
                {
                    element.Type = "Binary";
                }

                if (!element.SupportsInitialValue)
                {
                    element.InitialValue = null;
                }

                break;

            case "MinLength":
                int newMinLength = (int)e.NewValue;

                if (element.Type != "String")
                {
                    element.MinLength = 0;
                }

                if (newMinLength < 0)
                {
                    errorMessages.Add($"{modelClass.Name}.{element.Name}: MinLength must be zero or a positive number");
                }

                break;

            case "MaxLength":
                int newMaxLength = (int)e.NewValue;

                if (element.Type != "String")
                {
                    element.MaxLength = 0;
                }

                if (newMaxLength < 0)
                {
                    errorMessages.Add($"{modelClass.Name}.{element.Name}: MaxLength must be zero or a positive number");
                }

                break;

            case "IdentityType":

                if (element.IsIdentity)
                {
                    if (element.IdentityType == IdentityType.None)
                    {
                        errorMessages.Add($"{modelClass.Name}.{element.Name}: Identity properties must have an identity type defined");
                    }
                    else
                    {
                        element.AutoProperty = true;
                    }
                }
                else if (!element.IsIdentity)
                {
                    element.IdentityType = IdentityType.None;
                }

                break;

            case "ReadOnly":

                if (!element.Persistent || element.SetterVisibility != SetterAccessModifier.Public)
                {
                    element.ReadOnly = false;
                }

                break;

            case "IsIdentity":
                bool newIsIdentity = (bool)e.NewValue;

                if (newIsIdentity)
                {
                    if (element.ModelClass.IsDependentType)
                    {
                        errorMessages.Add($"{modelClass.Name}.{element.Name}: Can't make {element.Name} an identity because {modelClass.Name} is a dependent type and can't have an identity property.");
                    }
                    else
                    {
                        if (!modelRoot.ValidIdentityAttributeTypes.Contains(element.Type))
                        {
                            errorMessages.Add($"{modelClass.Name}.{element.Name}: Properties of type {element.Type} can't be used as identity properties.");
                        }
                        else
                        {
                            element.IsConcurrencyToken = false;
                            element.Indexed            = true;
                            element.IndexedUnique      = true;
                            element.Persistent         = true;
                            element.Required           = true;

                            if (element.IdentityType == IdentityType.None)
                            {
                                element.IdentityType = IdentityType.AutoGenerated;
                            }
                        }
                    }
                }
                else
                {
                    element.IdentityType = IdentityType.None;
                }

                break;

            case "IsConcurrencyToken":
                bool newIsConcurrencyToken = (bool)e.NewValue;

                if (newIsConcurrencyToken)
                {
                    element.IsIdentity = false;
                    element.Persistent = true;
                    element.Required   = true;
                    element.Type       = "Binary";
                }

                break;

            case "Required":
                bool newRequired = (bool)e.NewValue;

                if (!newRequired)
                {
                    if (element.IsIdentity || element.IsConcurrencyToken)
                    {
                        element.Required = true;
                    }
                }

                break;

            case "Persistent":
                bool newPersistent = (bool)e.NewValue;

                if (!newPersistent)
                {
                    element.IsIdentity         = false;
                    element.Indexed            = false;
                    element.IndexedUnique      = false;
                    element.IdentityType       = IdentityType.None;
                    element.IsConcurrencyToken = false;
                    element.Virtual            = false;
                }

                break;

            case "Name":
                string newName = (string)e.NewValue;

                if (string.IsNullOrEmpty(newName))
                {
                    errorMessages.Add("Name must be a valid .NET identifier");
                }
                else
                {
                    ParseResult fragment;

                    try
                    {
                        fragment = ModelAttribute.Parse(element.ModelClass.ModelRoot, newName);

                        if (fragment == null)
                        {
                            errorMessages.Add($"{modelClass.Name}: Could not parse entry '{newName}'");
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(fragment.Name) || !CodeGenerator.IsValidLanguageIndependentIdentifier(fragment.Name))
                            {
                                errorMessages.Add($"{modelClass.Name}: Property name '{fragment.Name}' isn't a valid .NET identifier");
                            }
                            else if (modelClass.AllAttributes.Except(new[] { element }).Any(x => x.Name == fragment.Name))
                            {
                                errorMessages.Add($"{modelClass.Name}: Property name '{fragment.Name}' already in use");
                            }
                            else if (modelClass.AllNavigationProperties().Any(p => p.PropertyName == fragment.Name))
                            {
                                errorMessages.Add($"{modelClass.Name}: Property name '{fragment.Name}' already in use");
                            }
                            else
                            {
                                element.Name = fragment.Name;

                                if (fragment.Type != null)
                                {
                                    element.Type = fragment.Type;
                                }

                                if (fragment.Required != null)
                                {
                                    element.Required = fragment.Required.Value;
                                }

                                if (fragment.MaxLength != null)
                                {
                                    element.MaxLength = fragment.MaxLength.Value;
                                }

                                if (fragment.InitialValue != null)
                                {
                                    element.InitialValue = fragment.InitialValue;
                                }

                                if (fragment.IsIdentity)
                                {
                                    element.IsIdentity = true; // don't reset to false if not entered as part of name
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        errorMessages.Add($"{modelClass.Name}: Could not parse entry '{newName}': {exception.Message}");
                    }
                }

                break;

            case "InitialValue":
                string newInitialValue = (string)e.NewValue;

                if (!element.IsValidInitialValue(null, newInitialValue))
                {
                    errorMessages.Add($"{modelClass.Name}.{element.Name}: {newInitialValue} isn't a valid value for {element.Type}");
                }

                break;
            }

            errorMessages = errorMessages.Where(m => m != null).ToList();

            if (errorMessages.Any())
            {
                current.Rollback();
                ErrorDisplay.Show(string.Join("\n", errorMessages));
            }
        }