示例#1
0
        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();
            }
        }
示例#2
0
      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 = 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.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);

            //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(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;
               }
            }
         }
      }