public static Dictionary <string, string> GetLookupPropertiesMappingDictionary(this Edmx edmx, Type entityClrType) { string entityName = entityClrType.Name; Dictionary <string, string> mappingDictionary = new Dictionary <string, string>(); foreach (NavigationProperty navigationProperty in edmx.GetLookupProperties(entityClrType)) { string relationship = GetAdjustedText(navigationProperty.Relationship); string fromRole = navigationProperty.FromRole; string toRole = navigationProperty.ToRole; LinqToEdmx.Model.Conceptual.Association association = edmx.GetConceptualAssociation(relationship); LinqToEdmx.Model.Conceptual.AssociationEnd fromAssociation = edmx.GetConceptualAssociationEnd(relationship, fromRole); LinqToEdmx.Model.Conceptual.AssociationEnd toAssociation = edmx.GetConceptualAssociationEnd(relationship, toRole); AssociationSetMapping entityTypesMappingAssociation = edmx.GetMappingAssociationSet(entityName, relationship); foreach (EndProperty endProperty in entityTypesMappingAssociation.EndProperties) { if (endProperty.Name == toRole) { string columnName = endProperty.ScalarProperties[0].ColumnName; mappingDictionary.Add(navigationProperty.Name, columnName); } } } return(mappingDictionary); }
public static EndProperty GetParentChildRelationEndProperty(this Edmx edmx, string parentEntityName, string childPropertyName) { NavigationProperty navigationProperty = edmx.GetNavigationProperty(parentEntityName, childPropertyName); string relationship = GetAdjustedText(navigationProperty.Relationship); string fromRole = navigationProperty.FromRole; string toRole = navigationProperty.ToRole; LinqToEdmx.Model.Conceptual.Association association = edmx.GetConceptualAssociation(relationship); LinqToEdmx.Model.Conceptual.AssociationEnd fromAssociation = edmx.GetConceptualAssociationEnd(relationship, fromRole); LinqToEdmx.Model.Conceptual.AssociationEnd toAssociation = edmx.GetConceptualAssociationEnd(relationship, toRole); string childEntityName = GetAdjustedText(toAssociation.Type); AssociationSetMapping entityTypesMappingAssociation = edmx.GetMappingAssociationSet(childEntityName, relationship); return(entityTypesMappingAssociation.EndProperties.Where(e => e.Name == fromRole).Single()); }
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); }
public static LinqToEdmx.Model.Conceptual.AssociationEnd GetConceptualAssociationEnd(this Edmx edmx, string relationship, string role) { LinqToEdmx.Model.Conceptual.Association association = edmx.GetConceptualAssociation(relationship); return(association.Ends.Where(e => e.Role == role).Single()); }