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 CreateStorageAssociationReferentialConstraintProperties(LinqToEdmx.Model.Storage.Association association, IEnumerable <AssociationProperty> properties, bool isParentEntity)
        {
            //  <ReferentialConstraint>
            //      <Principal Role="Category">
            //          <PropertyRef Name="CategoryId" />
            //      </Principal>
            //      <Dependent Role="Product">
            //          <PropertyRef Name="CategoryId" />
            //      </Dependent>
            //  </ReferentialConstraint>
            foreach (var property in properties)
            {
                var principalProp = !isParentEntity ? property.Property : property.ForeignProperty;
                var dependentProp = !isParentEntity ? property.ForeignProperty : property.Property;

                var principalProperty = association.ReferentialConstraint.Principal.PropertyRefs.Where(p => p.Name == principalProp.KeyName).FirstOrDefault();
                if (principalProperty == null)
                {
                    principalProperty = new PropertyRef()
                    {
                        Name = principalProp.KeyName
                    };
                    association.ReferentialConstraint.Principal.PropertyRefs.Add(principalProperty);
                }

                var dependentProperty = association.ReferentialConstraint.Dependent.PropertyRefs.Where(p => p.Name == dependentProp.KeyName).FirstOrDefault();
                if (dependentProperty == null)
                {
                    dependentProperty = new PropertyRef()
                    {
                        Name = dependentProp.KeyName
                    };
                    association.ReferentialConstraint.Dependent.PropertyRefs.Add(dependentProperty);
                }
            }
        }
        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 AssociationEnd CreateStorageAssociationEnd(IEntity entity, string role, LinqToEdmx.Model.Storage.Association assocication, bool isCascadeDelete)
        {
            //  <End Role="Category" Type="PetShopModel1.Store.Category" Multiplicity="1">
            //     <OnDelete Action="Cascade" />
            //  </End>
            //  <End Role="Product" Type="PetShopModel1.Store.Product" Multiplicity="*" />
            var end = new AssociationEnd()
            {
                Role = role,
                Type = String.Concat(StorageSchema.Namespace, ".", entity.EntityKeyName)
            };

            if (isCascadeDelete)
            {
                end.OnDelete.Add(new OnAction()
                {
                    Action = EdmxConstants.OnDeleteActionCascade
                });
            }

            assocication.Ends.Add(end);

            return(end);
        }