public override void ElementAdded(ElementAddedEventArgs e) { base.ElementAdded(e); Association element = (Association)e.ModelElement; Store store = element.Store; Transaction current = store.TransactionManager.CurrentTransaction; if (current.IsSerializing || ModelRoot.BatchUpdating) { return; } if (e.ModelElement is UnidirectionalAssociation u) { ConfigureNewAssociation(u); } else if (e.ModelElement is BidirectionalAssociation b) { ConfigureNewAssociation(b); } AssociationChangedRules.SetEndpointRoles(element); PresentationHelper.UpdateAssociationDisplay(element); }
private void ProcessUnidirectionalAssociations(ParsingModels.ModelClass modelClass) { List<ModelUnidirectionalAssociation> unidirectionalAssociations = modelClass.UnidirectionalAssociations; foreach (ModelUnidirectionalAssociation data in unidirectionalAssociations) { if (Store.ModelRoot().EntityFrameworkVersion == EFVersion.EF6 && data.SourceMultiplicity != ParsingModels.Multiplicity.ZeroMany && data.TargetMultiplicity != ParsingModels.Multiplicity.ZeroMany) { data.ForeignKey = null; } UnidirectionalAssociation existing = Store.GetAll<UnidirectionalAssociation>() .FirstOrDefault(x => x.Target.Name == data.TargetClassName && x.Source.Name == data.SourceClassName && x.Source.Name == modelClass.Name // just to be sure && x.TargetPropertyName == data.TargetPropertyName); if (existing != null) { if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey)) { existing.FKPropertyName = data.ForeignKey; existing.Source.ModelRoot.ExposeForeignKeys = true; } continue; } ModelClass source = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.SourceClassFullName); ModelClass target = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.TargetClassFullName); if (source == null || target == null || source.FullName != modelClass.FullName) continue; // ReSharper disable once UnusedVariable UnidirectionalAssociation element = new UnidirectionalAssociation(Store, new[] { new RoleAssignment(UnidirectionalAssociation.UnidirectionalSourceDomainRoleId, source), new RoleAssignment(UnidirectionalAssociation.UnidirectionalTargetDomainRoleId, target) }, new[] { new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, ConvertMultiplicity(data.SourceMultiplicity)), new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, ConvertMultiplicity(data.TargetMultiplicity)), new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, data.TargetPropertyName), new PropertyAssignment(Association.TargetSummaryDomainPropertyId, data.TargetSummary), new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, data.TargetDescription), new PropertyAssignment(Association.FKPropertyNameDomainPropertyId, data.ForeignKey), new PropertyAssignment(Association.SourceRoleDomainPropertyId, ConvertRole(data.SourceRole)), new PropertyAssignment(Association.TargetRoleDomainPropertyId, ConvertRole(data.TargetRole)), }); AssociationChangedRules.SetEndpointRoles(element); } }
private List<ModelElement> ProcessRootData(ParsingModels.ModelRoot rootData) { List<ModelElement> result = new List<ModelElement>(); ModelRoot modelRoot = Store.ModelRoot(); modelRoot.EntityContainerName = rootData.EntityContainerName; modelRoot.Namespace = rootData.Namespace; result.AddRange(ProcessClasses(modelRoot, rootData.Classes)); result.AddRange(ProcessEnumerations(modelRoot, rootData.Enumerations)); foreach (Association association in modelRoot.Store.GetAll<Association>()) { AssociationChangedRules.SetEndpointRoles(association); AssociationChangedRules.FixupForeignKeys(association); } return result; }
public override void ElementAdded(ElementAddedEventArgs e) { base.ElementAdded(e); Association element = (Association)e.ModelElement; Store store = element.Store; Transaction current = store.TransactionManager.CurrentTransaction; PluralizationService pluralizationService = ModelRoot.PluralizationService; if (current.IsSerializing || ModelRoot.BatchUpdating) { return; } // add unidirectional // source can't be dependent (connection builder handles this) // if target is dependent, // source cardinality is 0..1 // target cardinality is 0..1 // source is principal if (element is UnidirectionalAssociation && element.Target.IsDependentType) { element.TargetMultiplicity = Multiplicity.ZeroOne; element.SourceRole = EndpointRole.Principal; element.TargetRole = EndpointRole.Dependent; } // add bidirectional // neither can be dependent (connection builder handles this) if (string.IsNullOrEmpty(element.TargetPropertyName)) { string rootName = element.TargetMultiplicity == Multiplicity.ZeroMany && pluralizationService?.IsSingular(element.Target.Name) == true ? pluralizationService.Pluralize(element.Target.Name) : element.Target.Name; string identifierName = rootName; int index = 0; while (element.Source.HasPropertyNamed(identifierName)) { identifierName = $"{rootName}_{++index}"; } element.TargetPropertyName = identifierName; } if (element is BidirectionalAssociation bidirectionalAssociation) { if (string.IsNullOrEmpty(bidirectionalAssociation.SourcePropertyName)) { string rootName = element.SourceMultiplicity == Multiplicity.ZeroMany && pluralizationService?.IsSingular(element.Source.Name) == true ? pluralizationService.Pluralize(element.Source.Name) : element.Source.Name; string identifierName = rootName; int index = 0; while (element.Target.HasPropertyNamed(identifierName)) { identifierName = $"{rootName}_{++index}"; } bidirectionalAssociation.SourcePropertyName = identifierName; } } AssociationChangedRules.SetEndpointRoles(element); PresentationHelper.UpdateAssociationDisplay(element); }
/// <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) { if (!modelRoot.IsEFCore5Plus) { 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 "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 (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)); } }
private void ProcessAssociation([NotNull] ModelClass source, [NotNull] ModelClass target, [NotNull] PropertyDeclarationSyntax propertyDecl, bool toMany = false) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } if (propertyDecl == null) { throw new ArgumentNullException(nameof(propertyDecl)); } using (Transaction tx = Store.TransactionManager.BeginTransaction("processing associations")) { string propertyName = propertyDecl.Identifier.ToString(); // since we don't have enough information from the code, we'll create unidirectional associations // cardinality 1 on the source end, 0..1 or 0..* on the target, depending on the parameter XMLDocumentation xmlDocumentation = new XMLDocumentation(propertyDecl); // if the association doesn't yet exist, create it if (!Store.ElementDirectory .AllElements .OfType <UnidirectionalAssociation>() .Any(a => a.Source == source && a.Target == target && a.TargetPropertyName == propertyName)) { // if there's a unidirectional going the other direction, we'll whack that one and make a bidirectional // otherwise, proceed as planned UnidirectionalAssociation compliment = Store.ElementDirectory .AllElements .OfType <UnidirectionalAssociation>() .FirstOrDefault(a => a.Source == target && a.Target == source); if (compliment == null) { UnidirectionalAssociation element = new UnidirectionalAssociation(Store, new[] { new RoleAssignment(UnidirectionalAssociation.UnidirectionalSourceDomainRoleId, source), new RoleAssignment(UnidirectionalAssociation.UnidirectionalTargetDomainRoleId, target) }, new[] { new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, Multiplicity.One), new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, toMany ? Multiplicity.ZeroMany : Multiplicity.ZeroOne), new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, propertyName), new PropertyAssignment(Association.TargetSummaryDomainPropertyId, xmlDocumentation.Summary), new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, xmlDocumentation.Description) }); AssociationChangedRules.SetEndpointRoles(element); } else { compliment.Delete(); BidirectionalAssociation element = new BidirectionalAssociation(Store, new[] { new RoleAssignment(BidirectionalAssociation.BidirectionalSourceDomainRoleId, source), new RoleAssignment(BidirectionalAssociation.BidirectionalTargetDomainRoleId, target) }, new[] { new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, compliment.TargetMultiplicity), new PropertyAssignment(BidirectionalAssociation.SourcePropertyNameDomainPropertyId, compliment.TargetPropertyName), new PropertyAssignment(BidirectionalAssociation.SourceSummaryDomainPropertyId, compliment.TargetSummary), new PropertyAssignment(BidirectionalAssociation.SourceDescriptionDomainPropertyId, compliment.TargetDescription), new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, toMany ? Multiplicity.ZeroMany : Multiplicity.ZeroOne), new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, propertyName), new PropertyAssignment(Association.TargetSummaryDomainPropertyId, xmlDocumentation.Summary), new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, xmlDocumentation.Description) }); AssociationChangedRules.SetEndpointRoles(element); } } tx.Commit(); } }
public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e) { base.ElementPropertyChanged(e); ModelRoot element = (ModelRoot)e.ModelElement; 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(); bool redraw = false; switch (e.DomainProperty.Name) { case "ConnectionString": if (e.NewValue != null) { element.ConnectionStringName = null; } break; case "ConnectionStringName": if (e.NewValue != null) { element.ConnectionString = null; } break; case "EntityFrameworkVersion": element.EntityFrameworkPackageVersion = "Latest"; if (element.EntityFrameworkVersion == EFVersion.EFCore) { element.InheritanceStrategy = CodeStrategy.TablePerHierarchy; } if (element.EntityFrameworkVersion == EFVersion.EF6) { List <Association> associations = store.ElementDirectory .AllElements .OfType <Association>() .Where(a => !string.IsNullOrEmpty(a.FKPropertyName) && a.SourceMultiplicity != Multiplicity.ZeroMany && a.TargetMultiplicity != Multiplicity.ZeroMany) .ToList(); string message = $"This will remove declared foreign key properties from {associations.Count} association{(associations.Count == 1 ? "" : "s")}. Are you sure?"; if (associations.Any() && BooleanQuestionDisplay.Show(store, message) == true) { foreach (Association association in associations) { association.FKPropertyName = null; AssociationChangedRules.FixupForeignKeys(association); } } } ModelRoot.ExecuteValidator?.Invoke(); break; case "EntityOutputDirectory": if (string.IsNullOrEmpty(element.EnumOutputDirectory) || element.EnumOutputDirectory == (string)e.OldValue) { element.EnumOutputDirectory = (string)e.NewValue; } if (string.IsNullOrEmpty(element.StructOutputDirectory) || element.StructOutputDirectory == (string)e.OldValue) { element.StructOutputDirectory = (string)e.NewValue; } break; case "EnumOutputDirectory": if (string.IsNullOrEmpty((string)e.NewValue) && !string.IsNullOrEmpty(element.EntityOutputDirectory)) { element.EnumOutputDirectory = element.EntityOutputDirectory; } break; case "ExposeForeignKeys": if (!element.ExposeForeignKeys) { foreach (Association association in element.Store.GetAll <Association>() .Where(a => (a.SourceRole == EndpointRole.Dependent || a.TargetRole == EndpointRole.Dependent) && !string.IsNullOrWhiteSpace(a.FKPropertyName))) { association.FKPropertyName = null; AssociationChangedRules.FixupForeignKeys(association); } } break; case "FileNameMarker": string newFileNameMarker = (string)e.NewValue; if (!Regex.Match($"a.{newFileNameMarker}.cs", @"^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\"";|/]+$") .Success) { errorMessages.Add("Invalid value to make part of file name"); } break; case "GridColor": foreach (EFModelDiagram diagram in element.GetDiagrams()) { diagram.GridColor = (Color)e.NewValue; } redraw = true; break; case "InheritanceStrategy": if ((element.EntityFrameworkVersion == EFVersion.EFCore) && (element.NuGetPackageVersion.MajorMinorVersionNum < 2.1)) { element.InheritanceStrategy = CodeStrategy.TablePerHierarchy; } break; case "Namespace": errorMessages.Add(CommonRules.ValidateNamespace((string)e.NewValue, CodeGenerator.IsValidLanguageIndependentIdentifier)); break; case "ShowCascadeDeletes": // Normally you'd think that we should be able to register this in a AssociateValueWith call // in AssociationConnector, but that doesn't appear to work. So call the update method here. foreach (Association association in store.ElementDirectory.FindElements <Association>()) { PresentationHelper.UpdateAssociationDisplay(association); } redraw = true; break; case "ShowGrid": foreach (EFModelDiagram diagram in element.GetDiagrams()) { diagram.ShowGrid = (bool)e.NewValue; } redraw = true; break; case "ShowWarningsInDesigner": redraw = true; if ((bool)e.NewValue) { ModelRoot.ExecuteValidator?.Invoke(); } break; case "SnapToGrid": foreach (EFModelDiagram diagram in element.GetDiagrams()) { diagram.SnapToGrid = (bool)e.NewValue; } redraw = true; break; case "StructOutputDirectory": if (string.IsNullOrEmpty((string)e.NewValue) && !string.IsNullOrEmpty(element.EntityOutputDirectory)) { element.StructOutputDirectory = element.EntityOutputDirectory; } break; case "WarnOnMissingDocumentation": if (element.ShowWarningsInDesigner) { redraw = true; } ModelRoot.ExecuteValidator?.Invoke(); break; } errorMessages = errorMessages.Where(m => m != null).ToList(); if (errorMessages.Any()) { current.Rollback(); ErrorDisplay.Show(store, string.Join("\n", errorMessages)); } if (redraw) { foreach (EFModelDiagram diagram in element.GetDiagrams().Where(d => d.ActiveDiagramView != null)) { diagram.Invalidate(true); } } }
private void ProcessBidirectionalAssociations(ParsingModels.ModelClass modelClass) { List<ModelBidirectionalAssociation> bidirectionalAssociations = modelClass.BidirectionalAssociations; foreach (ModelBidirectionalAssociation data in bidirectionalAssociations) { if (Store.ModelRoot().EntityFrameworkVersion == EFVersion.EF6 && data.SourceMultiplicity != ParsingModels.Multiplicity.ZeroMany && data.TargetMultiplicity != ParsingModels.Multiplicity.ZeroMany) data.ForeignKey = null; BidirectionalAssociation existing = Store.GetAll<BidirectionalAssociation>() .FirstOrDefault(x => x.Target.Name == data.TargetClassName && x.Source.Name == data.SourceClassName && x.Source.Name == modelClass.Name // just to be sure && x.TargetPropertyName == data.TargetPropertyName && x.SourcePropertyName == data.SourcePropertyName) ?? Store.GetAll<BidirectionalAssociation>() .FirstOrDefault(x => x.Source.Name == data.TargetClassName && x.Target.Name == data.SourceClassName && x.Target.Name == modelClass.Name // just to be sure && x.SourcePropertyName == data.TargetPropertyName && x.TargetPropertyName == data.SourcePropertyName); if (existing != null) { if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey)) { existing.FKPropertyName = string.Join(",", data.ForeignKey.Split(',').ToList().Select(p => p.Split('/').Last().Split(' ').Last())); existing.Source.ModelRoot.ExposeForeignKeys = true; } continue; } ModelClass source = Store.GetAll<ModelClass>().FirstOrDefault(c => c.Name == data.SourceClassName); ModelClass target = Store.GetAll<ModelClass>().FirstOrDefault(c => c.Name == data.TargetClassName); if (source == null || target == null || source.FullName != modelClass.FullName) continue; BidirectionalAssociation elementLink = (BidirectionalAssociation)BidirectionalAssociationBuilder.Connect(source, target); elementLink.SourceMultiplicity = ConvertMultiplicity(data.SourceMultiplicity); elementLink.TargetMultiplicity = ConvertMultiplicity(data.TargetMultiplicity); elementLink.TargetPropertyName = data.TargetPropertyName; elementLink.TargetSummary = data.TargetSummary; elementLink.TargetDescription = data.TargetDescription; elementLink.FKPropertyName = data.ForeignKey; elementLink.SourceRole = ConvertRole(data.SourceRole); elementLink.TargetRole = ConvertRole(data.TargetRole); elementLink.SourcePropertyName = data.SourcePropertyName; elementLink.SourceSummary = data.SourceSummary; elementLink.SourceDescription = data.SourceDescription; // ReSharper disable once UnusedVariable //BidirectionalAssociation element = new BidirectionalAssociation(Store, // new[] // { // new RoleAssignment(BidirectionalAssociation.BidirectionalSourceDomainRoleId, source), // new RoleAssignment(BidirectionalAssociation.BidirectionalTargetDomainRoleId, target) // }, // new[] // { // new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, ConvertMultiplicity(data.SourceMultiplicity)), // new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, ConvertMultiplicity(data.TargetMultiplicity)), // new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, data.TargetPropertyName), // new PropertyAssignment(Association.TargetSummaryDomainPropertyId, data.TargetSummary), // new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, data.TargetDescription), // new PropertyAssignment(Association.FKPropertyNameDomainPropertyId, data.ForeignKey), // new PropertyAssignment(Association.SourceRoleDomainPropertyId, ConvertRole(data.SourceRole)), // new PropertyAssignment(Association.TargetRoleDomainPropertyId, ConvertRole(data.TargetRole)), // new PropertyAssignment(BidirectionalAssociation.SourcePropertyNameDomainPropertyId, data.SourcePropertyName), // new PropertyAssignment(BidirectionalAssociation.SourceSummaryDomainPropertyId, data.SourceSummary), // new PropertyAssignment(BidirectionalAssociation.SourceDescriptionDomainPropertyId, data.SourceDescription), // }); AssociationChangedRules.SetEndpointRoles(elementLink); AssociationChangedRules.FixupForeignKeys(elementLink); // we could have a situation where there are no roles assigned (if 0/1-0/1 or 1-1). If we have exposed foreign keys, though, we can figure those out. if ((elementLink.SourceMultiplicity != Multiplicity.ZeroMany || elementLink.TargetMultiplicity != Multiplicity.ZeroMany) && (elementLink.SourceRole == EndpointRole.NotSet || elementLink.TargetRole == EndpointRole.NotSet) && !string.IsNullOrEmpty(elementLink.FKPropertyName)) { // which, if any, end has the foreign key properties in it? string firstFKPropertyName = elementLink.FKPropertyName.Split(',').First(); if (elementLink.Source.AllPropertyNames.Contains(firstFKPropertyName)) { elementLink.SourceRole = EndpointRole.Dependent; elementLink.TargetRole = EndpointRole.Principal; } else if (elementLink.Target.AllPropertyNames.Contains(firstFKPropertyName)) { elementLink.TargetRole = EndpointRole.Dependent; elementLink.SourceRole = EndpointRole.Principal; } } } }
private void ProcessUnidirectionalAssociations(ParsingModels.ModelClass modelClass) { List<ModelUnidirectionalAssociation> unidirectionalAssociations = modelClass.UnidirectionalAssociations; foreach (ModelUnidirectionalAssociation data in unidirectionalAssociations) { if (Store.ModelRoot().EntityFrameworkVersion == EFVersion.EF6 && data.SourceMultiplicity != ParsingModels.Multiplicity.ZeroMany && data.TargetMultiplicity != ParsingModels.Multiplicity.ZeroMany) { data.ForeignKey = null; } UnidirectionalAssociation existing = Store.GetAll<UnidirectionalAssociation>() .FirstOrDefault(x => x.Target.FullName == data.TargetClassFullName && x.Source.FullName == data.SourceClassFullName && x.Source.FullName == modelClass.FullName // just to be sure && x.TargetPropertyName == data.TargetPropertyName); if (existing != null) { if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey)) { existing.FKPropertyName = data.ForeignKey; existing.Source.ModelRoot.ExposeForeignKeys = true; } continue; } ModelClass source = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.SourceClassFullName); ModelClass target = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.TargetClassFullName); if (source == null || target == null || source.FullName != modelClass.FullName) continue; UnidirectionalAssociation elementLink = (UnidirectionalAssociation)UnidirectionalAssociationBuilder.Connect(source, target); elementLink.SourceMultiplicity = ConvertMultiplicity(data.SourceMultiplicity); elementLink.TargetMultiplicity = ConvertMultiplicity(data.TargetMultiplicity); elementLink.TargetPropertyName = data.TargetPropertyName; elementLink.TargetSummary = data.TargetSummary; elementLink.TargetDescription = data.TargetDescription; elementLink.FKPropertyName = data.ForeignKey; elementLink.SourceRole = ConvertRole(data.SourceRole); elementLink.TargetRole = ConvertRole(data.TargetRole); AssociationChangedRules.SetEndpointRoles(elementLink); AssociationChangedRules.FixupForeignKeys(elementLink); // we could have a situation where there are no roles assigned (if 0/1-0/1 or 1-1). If we have exposed foreign keys, though, we can figure those out. if ((elementLink.SourceMultiplicity != Multiplicity.ZeroMany || elementLink.TargetMultiplicity != Multiplicity.ZeroMany) && (elementLink.SourceRole == EndpointRole.NotSet || elementLink.TargetRole == EndpointRole.NotSet) && !string.IsNullOrEmpty(elementLink.FKPropertyName)) { // which, if any, end has the foreign key properties in it? string firstFKPropertyName = elementLink.FKPropertyName.Split(',').First(); if (elementLink.Source.AllPropertyNames.Contains(firstFKPropertyName)) { elementLink.SourceRole = EndpointRole.Dependent; elementLink.TargetRole = EndpointRole.Principal; } else if (elementLink.Target.AllPropertyNames.Contains(firstFKPropertyName)) { elementLink.TargetRole = EndpointRole.Dependent; elementLink.SourceRole = EndpointRole.Principal; } } } }