Ejemplo n.º 1
0
        private static void ResolveAssociationValues(IAssociation association, out IEntity principalEntity, out IEntity dependentEntity, out bool isParentEntity)
        {
            bool isManyToManyEntity = association.IsParentManyToMany();

            principalEntity = !isManyToManyEntity ? association.Entity : association.ForeignEntity;
            dependentEntity = !isManyToManyEntity ? association.ForeignEntity : association.Entity;

            isParentEntity = association.IsParentEntity;
            if (association.AssociationType == AssociationType.ManyToMany)
            {
                isParentEntity &= association.IsParentManyToMany();
            }
        }
        private static void ResolveStorageAssociationValues(IAssociation association, out IEntity principalEntity, out IEntity dependentEntity, out bool isParentEntity, out string keyName, out string toRole, out string fromRole)
        {
            bool isManyToManyEntity = association.IsParentManyToMany();

            principalEntity = !isManyToManyEntity ? association.Entity : association.ForeignEntity;
            dependentEntity = !isManyToManyEntity ? association.ForeignEntity : association.Entity;

            toRole   = principalEntity.EntityKeyName;
            fromRole = dependentEntity.EntityKeyName;
            if (toRole.Equals(fromRole))
            {
                fromRole += 1;
            }

            keyName = isManyToManyEntity ? association.Entity.EntityKeyName : association.AssociationKeyName;

            isParentEntity = association.IsParentEntity;
            if (association.AssociationType == AssociationType.ManyToMany)
            {
                isParentEntity &= association.IsParentManyToMany();
            }
        }
Ejemplo n.º 3
0
        private static bool ExcludeAssociation(IAssociation association)
        {
            foreach (var property in association.Properties)
            {
                var invalid = association.IsParentEntity && !association.IsParentManyToMany()
                    ? (property.Property.PropertyType & PropertyType.Key) != PropertyType.Key || (property.ForeignProperty.PropertyType & PropertyType.Foreign) != PropertyType.Foreign
                    : (property.ForeignProperty.PropertyType & PropertyType.Key) != PropertyType.Key || (property.Property.PropertyType & PropertyType.Foreign) != PropertyType.Foreign;

                if (invalid)
                {
                    return(true);
                }
            }

            return(false);
        }
        private void CreateStorageAssociation(IAssociation association)
        {
            //<Association Name="FK__Product__Categor__0CBAE877">
            //  <End Role="Category" Type="PetShopModel1.Store.Category" Multiplicity="1" />
            //  <End Role="Product" Type="PetShopModel1.Store.Product" Multiplicity="*" />
            //  <ReferentialConstraint>
            //      <Principal Role="Category">
            //          <PropertyRef Name="CategoryId" />
            //      </Principal>
            //      <Dependent Role="Product">
            //          <PropertyRef Name="CategoryId" />
            //      </Dependent>
            //  </ReferentialConstraint>
            //</Association>
            IEntity principalEntity;
            IEntity dependentEntity;
            bool isParentEntity;
            string key;
            string toRole;
            string fromRole;
            ResolveStorageAssociationValues(association, out principalEntity, out dependentEntity, out isParentEntity, out key, out toRole, out fromRole);

            // The associations are stupid and if an end is added after the ReferentialConstraint, than the API doesn't detect it...
            var assoc = StorageSchema.Associations.Where(a => a.Name.Equals(association.AssociationKeyName)).FirstOrDefault();
            if (assoc != null)
                StorageSchema.Associations.Remove(assoc);
            assoc = new LinqToEdmx.Model.Storage.Association() {
                Name = association.AssociationKeyName,
                Ends = new List<AssociationEnd>()
            };

            StorageSchema.Associations.Add(assoc);

            var principalEnd = CreateStorageAssociationEnd(principalEntity, toRole, assoc, IsCascadeDelete(association));
            var dependentEnd = CreateStorageAssociationEnd(dependentEntity, fromRole, assoc, false);

            UpdateStorageAssociationEndMultiplicity(association, principalEnd, dependentEnd);

            assoc.ReferentialConstraint = new LinqToEdmx.Model.Storage.Constraint {
                Principal = new ReferentialConstraintRoleElement() {
                    Role = toRole
                },
                Dependent = new ReferentialConstraintRoleElement() {
                    Role = fromRole
                }
            };

            CreateStorageAssociationReferentialConstraintProperties(assoc, association.Properties, association.IsParentManyToMany());

            _storageAssociations.Add(association.AssociationKeyName);
        }
        private static void ResolveStorageAssociationValues(IAssociation association, out IEntity principalEntity, out IEntity dependentEntity, out bool isParentEntity, out string keyName, out string toRole, out string fromRole)
        {
            bool isManyToManyEntity = association.IsParentManyToMany();
            principalEntity = !isManyToManyEntity ? association.Entity : association.ForeignEntity;
            dependentEntity = !isManyToManyEntity ? association.ForeignEntity : association.Entity;

            toRole = principalEntity.EntityKeyName;
            fromRole = dependentEntity.EntityKeyName;
            if (toRole.Equals(fromRole))
                fromRole += 1;

            keyName = isManyToManyEntity ? association.Entity.EntityKeyName : association.AssociationKeyName;

            isParentEntity = association.IsParentEntity;
            if (association.AssociationType == AssociationType.ManyToMany)
                isParentEntity &= association.IsParentManyToMany();
        }
        private void ResolveConceptualAssociationValues(IAssociation association, out IEntity principalEntity, out IEntity dependentEntity, out bool isParentEntity, out string keyName, out string toRole, out string fromRole)
        {
            bool isManyToManyEntity = association.IsParentManyToMany();
            principalEntity = !isManyToManyEntity ? association.Entity : association.ForeignEntity;
            dependentEntity = !isManyToManyEntity ? (association.AssociationType == AssociationType.ManyToMany) ? association.IntermediaryAssociation.Entity : association.ForeignEntity : association.IntermediaryAssociation.ForeignEntity;

            toRole = ResolveEntityMappedName(principalEntity.EntityKey(), principalEntity.Name);
            fromRole = ResolveEntityMappedName(dependentEntity.EntityKey(), dependentEntity.Name);
            if (toRole.Equals(fromRole)) fromRole += 1;

            keyName = ResolveAssociationMappedName(isManyToManyEntity ? association.Entity.EntityKeyName : association.AssociationKeyName);

            isParentEntity = association.IsParentEntity;
            if (association.AssociationType == AssociationType.ManyToMany)
                isParentEntity &= association.IsParentManyToMany();
        }
        private void CreateConceptualNavigationProperty(EntityType entity, IAssociation association)
        {
            IEntity principalEntity;
            IEntity dependentEntity;
            bool isParentEntity;
            string key;
            string toRole;
            string fromRole;
            ResolveConceptualAssociationValues(association, out principalEntity, out dependentEntity, out isParentEntity, out key, out toRole, out fromRole);

            //11/4/2012 If an entity has an association to itself then we will modify the key if it's the child association.
            bool isSelfReferencingChild = !isParentEntity && principalEntity == dependentEntity;
            if (!isSelfReferencingChild && !association.IsParentManyToMany())
            {
                var temp = fromRole;
                fromRole = toRole;
                toRole = temp;
            }

            // 11/4/2012 Updated to check to see if a self referencing entity exists then we check the too and from roles. By checking only the to and from roles if it's a self referencing this allows custom names to be picked up.
            var navigationProperty = principalEntity == dependentEntity
                ? entity.NavigationProperties.FirstOrDefault(n => n.Relationship.Equals(String.Concat(ConceptualSchema.Namespace, ".", key)) && n.ToRole.Equals(toRole) && n.FromRole.Equals(fromRole))
                : entity.NavigationProperties.FirstOrDefault(n => n.Relationship.Equals(String.Concat(ConceptualSchema.Namespace, ".", key)));

            if (navigationProperty == null)
            {
                navigationProperty = new NavigationProperty() { Name = association.Name };
                entity.NavigationProperties.Add(navigationProperty);
            }

            if (String.IsNullOrEmpty(navigationProperty.Name) || association.Name.StartsWith(navigationProperty.Name))
                navigationProperty.Name = association.Name;

            navigationProperty.Relationship = String.Concat(ConceptualSchema.Namespace, ".", key);
            navigationProperty.FromRole = fromRole;
            navigationProperty.ToRole = toRole;
        }
        private void CreateConceptualAssociation(IAssociation association)
        {
            IEntity principalEntity;
            IEntity dependentEntity;
            bool isParentEntity;
            string key;
            string toRole;
            string fromRole;
            ResolveConceptualAssociationValues(association, out principalEntity, out dependentEntity, out isParentEntity, out key, out toRole, out fromRole);

            if (_conceptualAssociations.Contains(key))
                return;
            //<Association Name="FK__Product__Categor__0CBAE877">
            //  <End Role="Category" Type="PetShopModel1.Category" Multiplicity="1" />
            //  <End Role="Product" Type="PetShopModel1.Product" Multiplicity="*" />
            //  <ReferentialConstraint>
            //      <Principal Role="Category">
            //          <PropertyRef Name="CategoryId" />
            //      </Principal>
            //      <Dependent Role="Product">
            //          <PropertyRef Name="CategoryId" />
            //      </Dependent>
            //  </ReferentialConstraint>
            //</Association>
            var assoc = ConceptualSchema.Associations.Where(a => a.Name.Equals(key)).FirstOrDefault();
            if (assoc != null) ConceptualSchema.Associations.Remove(assoc);
            assoc = new LinqToEdmx.Model.Conceptual.Association()
                            {
                                Name = key,
                                Ends = new List<AssociationEnd>()
                            };

            ConceptualSchema.Associations.Add(assoc);

            var principalEnd = CreateConceptualAssociationEnd(principalEntity, toRole, assoc, false);
            var dependentEnd = CreateConceptualAssociationEnd(dependentEntity, fromRole, assoc, false);

            UpdateConceptualAssociationEndMultiplicity(association, principalEnd, dependentEnd);

            #region ReferentialConstraint

            if (!association.IsParentManyToMany())
            {
                assoc.ReferentialConstraint = new Constraint
                {
                    Principal = new ReferentialConstraintRoleElement() { Role = toRole },
                    Dependent = new ReferentialConstraintRoleElement() { Role = fromRole }
                };

                CreateStorageAssociationReferentialConstraintProperties(assoc, association.Properties, principalEntity, dependentEntity, false);
            }

            #endregion

            _conceptualAssociations.Add(key);
        }
Ejemplo n.º 9
0
        private static void ResolveAssociationValues(IAssociation association, out IEntity principalEntity, out IEntity dependentEntity, out bool isParentEntity)
        {
            bool isManyToManyEntity = association.IsParentManyToMany();
            principalEntity = !isManyToManyEntity ? association.Entity : association.ForeignEntity;
            dependentEntity = !isManyToManyEntity ? association.ForeignEntity : association.Entity;

            isParentEntity = association.IsParentEntity;
            if (association.AssociationType == AssociationType.ManyToMany)
                isParentEntity &= association.IsParentManyToMany();
        }
Ejemplo n.º 10
0
        private static bool ExcludeAssociation(IAssociation association)
        {
            foreach (var property in association.Properties)
            {
                var invalid = association.IsParentEntity && !association.IsParentManyToMany()
                    ? (property.Property.PropertyType & PropertyType.Key) != PropertyType.Key || (property.ForeignProperty.PropertyType & PropertyType.Foreign) != PropertyType.Foreign
                    : (property.ForeignProperty.PropertyType & PropertyType.Key) != PropertyType.Key || (property.Property.PropertyType & PropertyType.Foreign) != PropertyType.Foreign;

                if (invalid)
                    return true;
            }

            return false;
        }
        private void CreateStorageAssociation(IAssociation association)
        {
            //<Association Name="FK__Product__Categor__0CBAE877">
            //  <End Role="Category" Type="PetShopModel1.Store.Category" Multiplicity="1" />
            //  <End Role="Product" Type="PetShopModel1.Store.Product" Multiplicity="*" />
            //  <ReferentialConstraint>
            //      <Principal Role="Category">
            //          <PropertyRef Name="CategoryId" />
            //      </Principal>
            //      <Dependent Role="Product">
            //          <PropertyRef Name="CategoryId" />
            //      </Dependent>
            //  </ReferentialConstraint>
            //</Association>
            IEntity principalEntity;
            IEntity dependentEntity;
            bool    isParentEntity;
            string  key;
            string  toRole;
            string  fromRole;

            ResolveStorageAssociationValues(association, out principalEntity, out dependentEntity, out isParentEntity, out key, out toRole, out fromRole);

            // The associations are stupid and if an end is added after the ReferentialConstraint, than the API doesn't detect it...
            var assoc = StorageSchema.Associations.Where(a => a.Name.Equals(association.AssociationKeyName)).FirstOrDefault();

            if (assoc != null)
            {
                StorageSchema.Associations.Remove(assoc);
            }
            assoc = new LinqToEdmx.Model.Storage.Association()
            {
                Name = association.AssociationKeyName,
                Ends = new List <AssociationEnd>()
            };

            StorageSchema.Associations.Add(assoc);

            var principalEnd = CreateStorageAssociationEnd(principalEntity, toRole, assoc, IsCascadeDelete(association));
            var dependentEnd = CreateStorageAssociationEnd(dependentEntity, fromRole, assoc, false);

            UpdateStorageAssociationEndMultiplicity(association, principalEnd, dependentEnd);

            assoc.ReferentialConstraint = new LinqToEdmx.Model.Storage.Constraint {
                Principal = new ReferentialConstraintRoleElement()
                {
                    Role = toRole
                },
                Dependent = new ReferentialConstraintRoleElement()
                {
                    Role = fromRole
                }
            };

            CreateStorageAssociationReferentialConstraintProperties(assoc, association.Properties, association.IsParentManyToMany());

            _storageAssociations.Add(association.AssociationKeyName);
        }