private void CreateStorageAssociationSet(IAssociation 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); //<AssociationSet Name="FK__Product__Categor__0CBAE877" Association="PetShopModel1.Store.FK__Product__Categor__0CBAE877"> // <End Role="Category" EntitySet="Category" /> // <End Role="Product" EntitySet="Product" /> //</AssociationSet> var associationSet = StorageSchemaEntityContainer.AssociationSets.Where(e => e.Name.Equals(association.AssociationKeyName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (associationSet == null) { associationSet = new EntityContainer.AssociationSetLocalType { Name = association.AssociationKeyName, Ends = new List <EntityContainer.AssociationSetLocalType.EndLocalType>() }; StorageSchemaEntityContainer.AssociationSets.Add(associationSet); } else { // Remove the AssociationEnd's that don't exist. var items = associationSet.Ends.Where(e => (!e.Role.Equals(toRole) || !e.Role.Equals(ResolveStorageEntityName(association.Entity.EntityKeyName))) && (!e.Role.Equals(fromRole) || !e.Role.Equals(ResolveStorageEntityName(association.ForeignEntity.EntityKeyName)))); foreach (var associationEnd in items) { associationSet.Ends.Remove(associationEnd); } } // Set or sync the default values. associationSet.Name = association.AssociationKeyName; associationSet.Association = String.Concat(StorageSchema.Namespace, ".", association.AssociationKeyName); var principalEnd = CreateStorageAssociationSetEnd(principalEntity, toRole, associationSet); var dependentEnd = CreateStorageAssociationSetEnd(dependentEntity, fromRole, associationSet, principalEnd); // Update the Ends (forces the ends to be grouped in the edmx file). associationSet.Ends = (from a in associationSet.Ends orderby a.Role select a).ToList(); }
private EntityContainer.AssociationSetLocalType.EndLocalType CreateStorageAssociationSetEnd(IEntity entity, string role, EntityContainer.AssociationSetLocalType set, EntityContainer.AssociationSetLocalType.EndLocalType otherEnd = null) { var end = otherEnd == null? set.Ends.Where(e => e.Role.Equals(role) || e.Role.Equals(ResolveStorageEntityName(entity.EntityKeyName))).FirstOrDefault() : set.Ends.Where(e => !e.Equals(otherEnd)).FirstOrDefault(); if (end == null) { end = new EntityContainer.AssociationSetLocalType.EndLocalType() { Role = role }; set.Ends.Add(end); } end.Role = role; end.EntitySet = entity.EntityKeyName; return(end); }