public static void PushDown(this ModelClass superclass, ModelClass subclass) { Store store = superclass.Store; using (Transaction transaction = store.TransactionManager.BeginTransaction("PushDown")) { List <ModelAttribute> newAttributes = superclass.AllAttributes .Select(modelAttribute => (ModelAttribute)modelAttribute.Copy(new[] { ClassHasAttributes.AttributeDomainRoleId })) .Distinct() .ToList(); foreach (ModelAttribute newAttribute in newAttributes) { newAttribute.ModelClass = subclass; } List <Association> associations = new List <Association>(); ModelClass src = superclass; while (src != null) { associations.AddRange(store.ElementDirectory.AllElements.OfType <Association>().Where(a => a.Source == src || a.Target == src)); src = src.Superclass; } associations = associations.Distinct().ToList(); foreach (UnidirectionalAssociation association in associations.OfType <UnidirectionalAssociation>()) { if (association.Source == superclass) { UnidirectionalAssociationBuilder.Connect(subclass, association.Target); } else { UnidirectionalAssociationBuilder.Connect(association.Source, subclass); } } foreach (BidirectionalAssociation association in associations.OfType <BidirectionalAssociation>()) { if (association.Source == superclass) { BidirectionalAssociationBuilder.Connect(subclass, association.Target); } else { BidirectionalAssociationBuilder.Connect(association.Source, subclass); } } transaction.Commit(); } }
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; } } } }