/// <summary> /// Method to set IsImplementNotifyTracking to false so that this instance of this tracking property is not /// storage-based. /// </summary> /// <param name="element"> /// The element on which to reset the property value. /// </param> internal void PreResetValue(ModelAttribute element) { // Force the IsImplementNotifyTracking property to false so that the value // of the ImplementNotify property is retrieved from storage. element.isImplementNotifyTrackingPropertyStorage = false; }
public static void AdjustEFCoreProperties(PropertyDescriptorCollection propertyDescriptors, ModelAttribute element) { ModelRoot modelRoot = element.ModelClass.ModelRoot; for (int index = 0; index < propertyDescriptors.Count; index++) { bool shouldRemove = false; switch (propertyDescriptors[index].Name) { case "PersistencePoint": shouldRemove = modelRoot.EntityFrameworkVersion == EFVersion.EF6; break; // add more as needed } if (shouldRemove) { propertyDescriptors.Remove(propertyDescriptors[index--]); } } }
public static void RemoveHiddenProperties(PropertyDescriptorCollection propertyDescriptors, ModelAttribute element) { //for (int index = 0; index < propertyDescriptors.Count; index++) //{ // bool shouldRemove = false; // switch (propertyDescriptors[index].Name) // { // default: // break; // } // if (shouldRemove) // propertyDescriptors.Remove(propertyDescriptors[index--]); //} }
internal void MoveAttribute(ModelAttribute attribute, ModelClass destination) { MergeDisconnect(attribute); destination.MergeRelate(attribute, null); }
/// <summary> /// Method to set IsDatabaseCollationTracking to false so that this instance of this tracking property is not /// storage-based. /// </summary> /// <param name="element"> /// The element on which to reset the property value. /// </param> internal void PreResetValue(ModelAttribute element) { // Force the IsDatabaseCollationTracking property to false so that the value // of the DatabaseCollation property is retrieved from storage. element.isDatabaseCollationTrackingPropertyStorage = false; }
private static bool CanAcceptModelAttributeAsTarget(ModelAttribute candidate) { return(CanAcceptModelClassAsTarget(candidate.ModelClass)); }
/// <summary> /// Get the embedding link to this element. /// Assumes there is no inheritance between embedding relationships. /// (If there is, you need to make sure you've got the relationship that is represented in the shape compartment.) /// </summary> /// <param name="child"></param> /// <returns></returns> private ElementLink GetEmbeddingLink(ModelAttribute child) => child.GetDomainClass() .AllEmbeddedByDomainRoles .SelectMany(role => role.OppositeDomainRole.GetElementLinks(child)) .FirstOrDefault();
/// <summary>Performs the reset operation for the IsColumnTypeTracking property for a model element.</summary> /// <param name="element">The model element that has the property to reset.</param> internal void ResetValue(ModelAttribute element) { element.isColumnTypeTrackingPropertyStorage = (element.ColumnType == "default"); }
/// <summary> /// Remember which item the mouse was dragged from. /// We don't create an Action immediately, as this would inhibit the /// inline text editing feature. Instead, we just remember the details /// and will create an Action when/if the mouse moves off this list item. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Compartment_MouseDown(object sender, DiagramMouseEventArgs e) { dragStartElement = e.HitDiagramItem.RepresentedElements.OfType <ModelAttribute>().FirstOrDefault(); compartmentBounds = e.HitDiagramItem.Shape.AbsoluteBoundingBox; }
/// <summary> /// User has released the mouse button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Compartment_MouseUp(object sender, DiagramMouseEventArgs e) => dragStartElement = null;
public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e) { base.ElementPropertyChanged(e); ModelAttribute element = (ModelAttribute)e.ModelElement; ModelClass modelClass = element.ModelClass; 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 (!ModelAttribute.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"; } 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 (!ModelAttribute.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)); } }
private static ElementLink ConnectModelClassToModelAttribute(ModelClass sourceAccepted, ModelAttribute targetAccepted) { return(ConnectModelClassToModelClass(sourceAccepted, targetAccepted.ModelClass)); }
private static bool CanAcceptModelClassAndModelAttributeAsSourceAndTarget(ModelClass sourceModelClass, ModelAttribute targetModelAttribute) { return(CanAcceptModelClassAndModelClassAsSourceAndTarget(sourceModelClass, targetModelAttribute.ModelClass)); }
private void ProcessProperties([NotNull] ClassDeclarationSyntax classDecl) { if (classDecl == null) { throw new ArgumentNullException(nameof(classDecl)); } Transaction tx = Store.TransactionManager.CurrentTransaction == null ? Store.TransactionManager.BeginTransaction() : null; try { string className = classDecl.Identifier.Text; ModelRoot modelRoot = Store.ModelRoot(); ModelClass modelClass = Store.Get <ModelClass>().FirstOrDefault(c => c.Name == className); modelClass.Attributes.Clear(); foreach (PropertyDeclarationSyntax propertyDecl in classDecl.DescendantNodes().OfType <PropertyDeclarationSyntax>()) { // if the property has a fat arrow expression as its direct descendent, it's a readonly calculated property // TODO: we should handle this // but for this release, ignore it if (propertyDecl.ChildNodes().OfType <ArrowExpressionClauseSyntax>().Any()) { continue; } AccessorDeclarationSyntax getAccessor = (AccessorDeclarationSyntax)propertyDecl.DescendantNodes().FirstOrDefault(node => node.IsKind(SyntaxKind.GetAccessorDeclaration)); AccessorDeclarationSyntax setAccessor = (AccessorDeclarationSyntax)propertyDecl.DescendantNodes().FirstOrDefault(node => node.IsKind(SyntaxKind.SetAccessorDeclaration)); // if there's no getAccessor, why are we bothering? if (getAccessor == null) { continue; } string propertyName = propertyDecl.Identifier.ToString(); string propertyType = propertyDecl.Type.ToString(); ModelClass target = modelRoot.Classes.FirstOrDefault(t => t.Name == propertyType); // is the property type a generic? // assume it's a list // TODO: this really isn't a good assumption. Fix later if (propertyDecl.ChildNodes().OfType <GenericNameSyntax>().Any()) { ProcessAsList(propertyDecl, className, propertyName, propertyType, modelRoot, modelClass); continue; } // is the property type an existing ModelClass? if (target != null) { ProcessAssociation(modelClass, target, propertyDecl); continue; } bool propertyShowsNullable = propertyDecl.DescendantNodes().OfType <NullableTypeSyntax>().Any(); // is the property type something we don't know about? if (!modelRoot.IsValidCLRType(propertyType) && ProcessUnknownType(propertyType, propertyShowsNullable, modelRoot, modelClass, propertyDecl)) { continue; } // if we're here, it's just a property (CLR or enum) try { // ReSharper disable once UseObjectOrCollectionInitializer ModelAttribute modelAttribute = new ModelAttribute(Store, new PropertyAssignment(ModelAttribute.NameDomainPropertyId, propertyName)) { Type = ModelAttribute.ToCLRType(propertyDecl.Type.ToString()).Trim('?'), Required = propertyDecl.HasAttribute("RequiredAttribute") || !propertyShowsNullable, Indexed = propertyDecl.HasAttribute("IndexedAttribute"), IsIdentity = propertyDecl.HasAttribute("KeyAttribute"), Virtual = propertyDecl.DescendantTokens().Any(t => t.IsKind(SyntaxKind.VirtualKeyword)) }; if (modelAttribute.Type.ToLower() == "string") { AttributeSyntax maxLengthAttribute = propertyDecl.GetAttribute("MaxLengthAttribute") ?? propertyDecl.GetAttribute("StringLengthAttribute"); AttributeArgumentSyntax maxLength = maxLengthAttribute?.GetAttributeArguments()?.FirstOrDefault(); if (maxLength != null) { modelAttribute.MaxLength = int.TryParse(maxLength.Expression.ToString(), out int _max) ? (int?)_max : null; } AttributeSyntax minLengthAttribute = propertyDecl.GetAttribute("MinLengthAttribute"); AttributeArgumentSyntax minLength = minLengthAttribute?.GetAttributeArguments()?.FirstOrDefault(); if (minLength != null) { modelAttribute.MinLength = int.TryParse(minLength.Expression.ToString(), out int _min) ? _min : 0; } } else { modelAttribute.MaxLength = null; modelAttribute.MinLength = 0; } // if no setAccessor, it's a calculated readonly property if (setAccessor == null) { modelAttribute.Persistent = false; modelAttribute.ReadOnly = true; } modelAttribute.AutoProperty = !getAccessor.DescendantNodes().Any(node => node.IsKind(SyntaxKind.Block)) && !setAccessor.DescendantNodes().Any(node => node.IsKind(SyntaxKind.Block)); modelAttribute.SetterVisibility = setAccessor.Modifiers.Any(m => m.ToString() == "protected") ? SetterAccessModifier.Protected : setAccessor.Modifiers.Any(m => m.ToString() == "internal") ? SetterAccessModifier.Internal : SetterAccessModifier.Public; AttributeSyntax columnAttribute = propertyDecl.GetAttribute("Column"); if (columnAttribute != null) { modelAttribute.ColumnName = columnAttribute.GetAttributeArguments().First().Expression.ToString().Trim('"'); string columnType = columnAttribute.GetNamedArgumentValue("TypeName"); if (columnType != null) { modelAttribute.ColumnType = columnType; } } XMLDocumentation xmlDocumentation = new XMLDocumentation(propertyDecl); modelAttribute.Summary = xmlDocumentation.Summary; modelAttribute.Description = xmlDocumentation.Description; modelClass.Attributes.Add(modelAttribute); } catch { WarningDisplay.Show($"Could not parse '{className}.{propertyDecl.Identifier}'."); } } } catch { tx = null; throw; } finally { tx?.Commit(); } void ProcessAsList(PropertyDeclarationSyntax propertyDecl, string className, string propertyName, string propertyType, ModelRoot modelRoot, ModelClass modelClass) { GenericNameSyntax genericDecl = propertyDecl.ChildNodes().OfType <GenericNameSyntax>().FirstOrDefault(); List <string> contentTypes = genericDecl.DescendantNodes().OfType <IdentifierNameSyntax>().Select(i => i.Identifier.ToString()).ToList(); // there can only be one generic argument if (contentTypes.Count == 1) { propertyType = contentTypes[0]; ModelClass target = modelRoot.Classes.FirstOrDefault(t => t.Name == propertyType); if (target == null) { target = new ModelClass(Store, new PropertyAssignment(ModelClass.NameDomainPropertyId, propertyType)); modelRoot.Classes.Add(target); } ProcessAssociation(modelClass, target, propertyDecl, true); } else { WarningDisplay.Show($"Found {className}.{propertyName}, but its type ({genericDecl.Identifier}<{string.Join(", ", contentTypes)}>) isn't anything expected. Ignoring..."); } } bool ProcessUnknownType(string propertyType, bool propertyShowsNullable, ModelRoot modelRoot, ModelClass modelClass, PropertyDeclarationSyntax propertyDecl) { // might be an enum. If so, we'll handle it like a CLR type // if it's nullable, it's definitely an enum, but if we don't know about it, it could be an enum or a class if (!KnownEnums.Contains(propertyType) && !propertyShowsNullable) { // assume it's a class and create the class ModelClass target = new ModelClass(Store, new PropertyAssignment(ModelClass.NameDomainPropertyId, propertyType)); modelRoot.Classes.Add(target); ProcessAssociation(modelClass, target, propertyDecl); return(true); } return(false); } }
/// <summary> /// Forget the source item if mouse up occurs outside the compartment. /// </summary> /// <param name="e"></param> public override void OnMouseUp(DiagramMouseEventArgs e) { base.OnMouseUp(e); dragStartElement = null; }
public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e) { base.ElementPropertyChanged(e); ModelAttribute element = (ModelAttribute)e.ModelElement; if (element.IsDeleted) { return; } ModelClass modelClass = element.ModelClass; ModelRoot modelRoot = element.Store.ModelRoot(); Store store = element.Store; Transaction current = store.TransactionManager.CurrentTransaction; if (current.IsSerializing || ModelRoot.BatchUpdating) { return; } if (Equals(e.NewValue, e.OldValue)) { return; } List <string> errorMessages = EFCoreValidator.GetErrors(element).ToList(); switch (e.DomainProperty.Name) { case "AutoProperty": { if (element.AutoProperty) { element.PersistencePoint = PersistencePointType.Property; element.ImplementNotify = false; } } 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.IdentityType = IdentityType.None; } } break; case "ImplementNotify": { if (element.IsIdentity) { element.ImplementNotify = false; } if (element.ImplementNotify) { element.AutoProperty = false; } } break; case "Indexed": { if (element.IsIdentity) { element.Indexed = true; } if (element.IsConcurrencyToken) { element.Indexed = false; } if (element.Indexed) { element.Persistent = true; } } break; case "InitialValue": { string newInitialValue = (string)e.NewValue; if (string.IsNullOrEmpty(newInitialValue)) { break; } // if the property is an Enum and the user just typed the name of the Enum value without the Enum type name, help them out if (element.ModelClass.ModelRoot.Enums.Any(x => x.Name == element.Type) && !newInitialValue.Contains(".")) { newInitialValue = element.InitialValue = $"{element.Type}.{newInitialValue}"; } if (!element.IsValidInitialValue(null, newInitialValue)) { errorMessages.Add($"{modelClass.Name}.{element.Name}: {newInitialValue} isn't a valid value for {element.Type}"); } } break; case "IsAbstract": { if ((bool)e.NewValue) { modelClass.IsAbstract = true; } } break; case "IsConcurrencyToken": { bool newIsConcurrencyToken = (bool)e.NewValue; if (newIsConcurrencyToken) { element.IsIdentity = false; element.Persistent = true; element.Required = true; element.Type = "Binary"; } } 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 "MinLength": { int minLengthValue = (int)e.NewValue; if (element.Type != "String") { element.MinLength = 0; } else if (minLengthValue < 0) { errorMessages.Add($"{modelClass.Name}.{element.Name}: MinLength must be zero or a positive number"); } else if (element.MaxLength > 0 && minLengthValue > element.MaxLength) { errorMessages.Add($"{modelClass.Name}.{element.Name}: MinLength cannot be greater than MaxLength"); } } break; case "MaxLength": { if (element.Type != "String") { element.MaxLength = null; } else { int?maxLengthValue = (int?)e.NewValue; if (maxLengthValue > 0 && element.MinLength > maxLengthValue) { errorMessages.Add($"{modelClass.Name}.{element.Name}: MinLength cannot be greater than MaxLength"); } } } 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(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; } element.MaxLength = fragment.MaxLength; element.MinLength = fragment.MinLength ?? 0; 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 "PersistencePoint": { if ((PersistencePointType)e.NewValue == PersistencePointType.Field) { element.AutoProperty = false; } } 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 "ReadOnly": { if (!element.Persistent || element.SetterVisibility != SetterAccessModifier.Public) { element.ReadOnly = false; } } break; case "Required": { bool newRequired = (bool)e.NewValue; if (!newRequired) { if (element.IsIdentity || element.IsConcurrencyToken) { element.Required = 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 = null; element.MinLength = 0; element.StringType = HTML5Type.None; } else { if (!element.IsValidInitialValue(newType)) { element.InitialValue = null; } element.MaxLength = ModelAttribute.GetDefaultStringLength?.Invoke(); } if (element.IsConcurrencyToken) { element.Type = "Binary"; } if (!element.SupportsInitialValue) { element.InitialValue = null; } } break; } errorMessages = errorMessages.Where(m => m != null).ToList(); if (errorMessages.Any()) { current.Rollback(); ErrorDisplay.Show(string.Join("\n", errorMessages)); } }
private void OnMenuAddProperties(object sender, EventArgs e) { NodeShape shapeElement = CurrentSelection.OfType <ClassShape>().FirstOrDefault(); if (shapeElement?.ModelElement is ModelClass element) { AddCodeForm codeForm = new AddCodeForm(element); if (codeForm.ShowDialog() == DialogResult.OK) { using (Transaction tx = element.Store.TransactionManager.BeginTransaction("AddProperties")) { element.Attributes.Clear(); foreach (string codeFormLine in codeForm.Lines) { try { ParseResult parseResult = ModelAttribute.Parse(element.ModelRoot, codeFormLine); if (parseResult == null) { Messages.AddWarning($"Could not parse '{codeFormLine}'. The line will be discarded."); continue; } string message = null; if (string.IsNullOrEmpty(parseResult.Name) || !CodeGenerator.IsValidLanguageIndependentIdentifier(parseResult.Name)) { message = $"Could not add '{parseResult.Name}' to {element.Name}: '{parseResult.Name}' is not a valid .NET identifier"; } else if (element.AllAttributes.Any(x => x.Name == parseResult.Name)) { message = $"Could not add {parseResult.Name} to {element.Name}: {parseResult.Name} already in use"; } else if (element.AllNavigationProperties().Any(p => p.PropertyName == parseResult.Name)) { message = $"Could not add {parseResult.Name} to {element.Name}: {parseResult.Name} already in use"; } if (message != null) { Messages.AddWarning(message); continue; } ModelAttribute modelAttribute = new ModelAttribute(element.Store, new PropertyAssignment(ModelAttribute.NameDomainPropertyId, parseResult.Name), new PropertyAssignment(ModelAttribute.TypeDomainPropertyId, parseResult.Type ?? "String"), new PropertyAssignment(ModelAttribute.RequiredDomainPropertyId, parseResult.Required ?? true), new PropertyAssignment(ModelAttribute.MaxLengthDomainPropertyId, parseResult.MaxLength ?? -1), new PropertyAssignment(ModelAttribute.MinLengthDomainPropertyId, parseResult.MinLength ?? 0), new PropertyAssignment(ModelAttribute.InitialValueDomainPropertyId, parseResult.InitialValue), new PropertyAssignment(ModelAttribute.IsIdentityDomainPropertyId, parseResult.IsIdentity), new PropertyAssignment(ModelAttribute.SetterVisibilityDomainPropertyId, parseResult.SetterVisibility ?? SetterAccessModifier.Public)); element.Attributes.Add(modelAttribute); } catch (Exception exception) { Messages.AddWarning($"Could not parse '{codeFormLine}'. {exception.Message}. The line will be discarded."); } } tx.Commit(); } } } }
public static void FixupForeignKeys(Association element) { List<ModelAttribute> fkProperties = element.Source.Attributes.Where(x => x.IsForeignKeyFor == element.Id) .Union(element.Target.Attributes.Where(x => x.IsForeignKeyFor == element.Id)) .ToList(); // EF6 can't have declared foreign keys for 1..1 / 0-1..1 / 1..0-1 / 0-1..0-1 relationships if (!string.IsNullOrEmpty(element.FKPropertyName) && element.Source.ModelRoot.EntityFrameworkVersion == EFVersion.EF6 && element.SourceMultiplicity != Multiplicity.ZeroMany && element.TargetMultiplicity != Multiplicity.ZeroMany) element.FKPropertyName = null; // if no FKs, remove all the attributes for this element if (string.IsNullOrEmpty(element.FKPropertyName) || element.Dependent == null) { List<ModelAttribute> unnecessaryProperties = fkProperties.Where(x => !x.IsIdentity).ToList(); if (unnecessaryProperties.Any()) WarningDisplay.Show($"{element.GetDisplayText()} doesn't specify defined foreign keys. Removing foreign key attribute(s) {string.Join(", ", unnecessaryProperties.Select(x => x.GetDisplayText()))}"); foreach (ModelAttribute attribute in unnecessaryProperties) { attribute.ClearFKMods(string.Empty); attribute.Delete(); } return; } // synchronize what's there to what should be there string[] currentForeignKeyPropertyNames = element.GetForeignKeyPropertyNames(); (IEnumerable<string> add, IEnumerable<ModelAttribute> remove) = fkProperties.Synchronize(currentForeignKeyPropertyNames, (attribute, name) => attribute.Name == name); List<ModelAttribute> removeList = remove.ToList(); fkProperties = fkProperties.Except(removeList).ToList(); // remove extras if (removeList.Any()) WarningDisplay.Show($"{element.GetDisplayText()} has extra foreign keys. Removing unnecessary foreign key attribute(s) {string.Join(", ", removeList.Select(x => x.GetDisplayText()))}"); for (int index = 0; index < removeList.Count; index++) { ModelAttribute attribute = removeList[index]; attribute.ClearFKMods(string.Empty); attribute.Delete(); removeList.RemoveAt(index--); } // reparent existing properties if needed foreach (ModelAttribute existing in fkProperties.Where(x => x.ModelClass != element.Dependent)) { existing.ClearFKMods(); existing.ModelClass.MoveAttribute(existing, element.Dependent); existing.SetFKMods(element); } // create new properties if they don't already exist foreach (string propertyName in add.Where(n => element.Dependent.Attributes.All(a => a.Name != n))) element.Dependent.Attributes.Add(new ModelAttribute(element.Store, new PropertyAssignment(ModelAttribute.NameDomainPropertyId, propertyName))); // make a pass through and fixup the types, summaries, etc. based on the principal's identity attributes ModelAttribute[] principalIdentityAttributes = element.Principal.AllIdentityAttributes.ToArray(); string summaryBoilerplate = element.GetSummaryBoilerplate(); for (int index = 0; index < currentForeignKeyPropertyNames.Length; index++) { ModelAttribute fkProperty = element.Dependent.Attributes.First(x => x.Name == currentForeignKeyPropertyNames[index]); ModelAttribute idProperty = principalIdentityAttributes[index]; bool required = element.Dependent == element.Source ? element.TargetMultiplicity == Multiplicity.One : element.SourceMultiplicity == Multiplicity.One; fkProperty.SetFKMods(element , summaryBoilerplate , required , idProperty.Type); } }
public override void ElementAdded(ElementAddedEventArgs e) { base.ElementAdded(e); Generalization element = (Generalization)e.ModelElement; Store store = element.Store; Transaction current = store.TransactionManager.CurrentTransaction; if (current.IsSerializing || ModelRoot.BatchUpdating) { return; } if (element.Subclass.IsPropertyBag && !element.Superclass.IsPropertyBag) { ErrorDisplay.Show(store, $"{element.Subclass.Name} -> {element.Superclass.Name}: Since {element.Subclass.Name} is a property bag, it can't inherit from {element.Superclass.Name}, which is not a property bag."); current.Rollback(); return; } if (element.IsInCircularInheritance()) { ErrorDisplay.Show(store, $"{element.Subclass.Name} -> {element.Superclass.Name}: That inheritance link would cause a circular reference."); current.Rollback(); return; } List <string> superclassPropertyNames = element.Superclass.AllPropertyNames.ToList(); List <string> nameClashes = element.Subclass .Attributes .Where(a => superclassPropertyNames.Contains(a.Name)) .Select(a => a.Name) .Union(element.Subclass .LocalNavigationProperties() .Where(p => p.PropertyName != null && superclassPropertyNames.Contains(p.PropertyName)) .Select(p => p.PropertyName)) .ToList(); // remove attributes in subclass that are present in superclass IF they are completely identical (except for ModelClass, of course) for (int i = 0; i < nameClashes.Count; i++) { ModelAttribute subclassAttribute = element.Subclass.Attributes.First(a => a.Name == nameClashes[i]); ModelAttribute superclassAttribute = element.Superclass.AllAttributes.First(a => a.Name == nameClashes[i]); List <(string propertyName, object thisValue, object otherValue)> differences = superclassAttribute.GetDifferences(subclassAttribute); // ignore these differences if found differences.RemoveAll(x => x.propertyName == "ModelClass" || x.propertyName == "Summary" || x.propertyName == "Description"); if (!differences.Any()) { element.Subclass.Attributes.Remove(element.Subclass.Attributes.Single(a => a.Name == nameClashes[i])); nameClashes.RemoveAt(i--); } } // if any remain with the same name, it's an error if (nameClashes.Any()) { string nameClashList = string.Join("\n ", nameClashes); ErrorDisplay.Show(store, $"{element.Subclass.Name} -> {element.Superclass.Name}: That inheritance link would cause name clashes. Resolve the following before setting the inheritance:\n {nameClashList}"); current.Rollback(); } }
/// <summary> /// Method to set IsColumnNameTracking to false so that this instance of this tracking property is not /// storage-based. /// </summary> /// <param name="element"> /// The element on which to reset the property value. /// </param> internal void PreResetValue(ModelAttribute element) { // Force the IsColumnNameTracking property to false so that the value // of the ColumnName property is retrieved from storage. element.isColumnNameTrackingPropertyStorage = false; }
/// <inheritdoc /> public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e) { base.ElementPropertyChanged(e); ModelAttribute element = (ModelAttribute)e.ModelElement; if (element.IsDeleted) { return; } ModelClass modelClass = element.ModelClass; ModelRoot modelRoot = element.Store.ModelRoot(); Store store = element.Store; Transaction current = store.TransactionManager.CurrentTransaction; if (current.IsSerializing || ModelRoot.BatchUpdating) { return; } if (Equals(e.NewValue, e.OldValue)) { return; } List <string> errorMessages = new List <string>(); switch (e.DomainProperty.Name) { case "AutoProperty": { if (element.AutoProperty) { element.PersistencePoint = PersistencePointType.Property; element.ImplementNotify = false; } else { if (string.IsNullOrEmpty(element.BackingFieldName)) { element.BackingFieldName = $"_{element.Name.ToCamelCase()}"; } } } 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"); } foreach (Association association in element.ModelClass.LocalNavigationProperties() .Where(nav => nav.AssociationObject.Dependent == element.ModelClass) .Select(nav => nav.AssociationObject) .Where(a => !string.IsNullOrWhiteSpace(a.FKPropertyName) && a.FKPropertyName.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Any(n => n.Trim() == element.Name))) { foreach (ModelAttribute attribute in association.GetFKAutoIdentityErrors()) { errorMessages.Add($"{association.Source.Name} <=> {association.Target.Name}: FK property {attribute.Name} in {association.Dependent.FullName} is an auto-generated identity. Migration will fail."); } } } else { element.IdentityType = IdentityType.None; } } break; case "ImplementNotify": { if (element.IsIdentity) { element.ImplementNotify = false; } if (element.ImplementNotify) { element.AutoProperty = false; } } break; case "Indexed": { if (element.IsIdentity) { element.Indexed = true; } if (element.IsConcurrencyToken) { element.Indexed = false; } if (element.Indexed) { element.Persistent = true; } } break; case "InitialValue": { string newInitialValue = (string)e.NewValue; if (string.IsNullOrEmpty(newInitialValue)) { break; } // if the property is an Enum and the user just typed the name of the Enum value without the Enum type name, help them out if (element.ModelClass.ModelRoot.Enums.Any(x => x.Name == element.Type) && !newInitialValue.Contains(".")) { newInitialValue = element.InitialValue = $"{element.Type}.{newInitialValue}"; } if (!element.IsValidInitialValue(null, newInitialValue)) { errorMessages.Add($"{modelClass.Name}.{element.Name}: {newInitialValue} isn't a valid value for {element.Type}"); } } break; case "IsAbstract": { if ((bool)e.NewValue) { modelClass.IsAbstract = true; } } break; case "IsConcurrencyToken": { bool newIsConcurrencyToken = (bool)e.NewValue; if (newIsConcurrencyToken) { element.IsIdentity = false; element.Persistent = true; element.Required = true; element.Type = "Binary"; } } break; case "IsIdentity": { if ((bool)e.NewValue) { 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.IsValidIdentityAttributeType(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; } } } foreach (Association association in element.ModelClass.LocalNavigationProperties() .Where(nav => nav.AssociationObject.Dependent == element.ModelClass) .Select(nav => nav.AssociationObject) .Where(a => !string.IsNullOrWhiteSpace(a.FKPropertyName) && a.FKPropertyName.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Any(n => n.Trim() == element.Name))) { association.GetFKAutoIdentityErrors(); } } else { element.IdentityType = IdentityType.None; } } break; case "MinLength": { int minLengthValue = (int)e.NewValue; if (element.Type != "String") { element.MinLength = 0; } else if (minLengthValue < 0) { errorMessages.Add($"{modelClass.Name}.{element.Name}: MinLength must be zero or a positive number"); } else if (element.MaxLength > 0 && minLengthValue > element.MaxLength) { errorMessages.Add($"{modelClass.Name}.{element.Name}: MinLength cannot be greater than MaxLength"); } } break; case "MaxLength": { if (element.Type != "String") { element.MaxLength = null; } else { int?maxLengthValue = (int?)e.NewValue; if (maxLengthValue > 0 && element.MinLength > maxLengthValue) { errorMessages.Add($"{modelClass.Name}.{element.Name}: MinLength cannot be greater than MaxLength"); } } } break; case "Name": { if (string.IsNullOrEmpty(element.Name) || !CodeGenerator.IsValidLanguageIndependentIdentifier(element.Name)) { errorMessages.Add($"{modelClass.Name}: Property name '{element.Name}' isn't a valid .NET identifier"); } if (modelClass.AllAttributes.Except(new[] { element }).Any(x => x.Name == element.Name) || modelClass.AllNavigationProperties().Any(p => p.PropertyName == element.Name)) { errorMessages.Add($"{modelClass.Name}: Property name '{element.Name}' already in use"); } } break; case "PersistencePoint": { if ((PersistencePointType)e.NewValue == PersistencePointType.Field) { element.AutoProperty = false; } } 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 "ReadOnly": { if (!element.Persistent || element.SetterVisibility != SetterAccessModifier.Public) { element.ReadOnly = false; } } break; case "Required": { bool newRequired = (bool)e.NewValue; if (!newRequired) { if (element.IsIdentity || element.IsConcurrencyToken) { element.Required = true; } } } break; case "Type": { string newType = (string)e.NewValue; if (element.IsIdentity) { if (!modelRoot.IsValidIdentityAttributeType(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; // Change type of any foreign key pointing to this class IEnumerable <Association> participatingAssociations = element.ModelClass.LocalNavigationProperties() .Where(nav => nav.AssociationObject.Dependent == element.ModelClass) .Select(nav => nav.AssociationObject) .Where(a => !string.IsNullOrWhiteSpace(a.FKPropertyName) && a.FKPropertyName.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Any(n => n.Trim() == element.Name)); foreach (Association association in participatingAssociations) { AssociationChangedRules.FixupForeignKeys(association); } } } if (newType != "String") { element.MaxLength = null; element.MinLength = 0; element.StringType = HTML5Type.None; } else { if (!element.IsValidInitialValue(newType)) { element.InitialValue = null; } //if (!modelClass.Store.InSerializationTransaction && !element.MaxLength.HasValue) // element.MaxLength = ModelAttribute.GetDefaultStringLength?.Invoke(); } if (element.IsConcurrencyToken) { element.Type = "Binary"; } if (!element.SupportsInitialValue) { element.InitialValue = null; } } break; } errorMessages = errorMessages.Where(m => m != null).ToList(); if (errorMessages.Any()) { current.Rollback(); ErrorDisplay.Show(store, string.Join("\n", errorMessages)); } }