public virtual void SetOwner(EntityType owner) { Util.ThrowIfReadOnly(this); if (owner == null) { _database.RemoveAssociationType(_associationType); } else { _associationType.TargetEnd = new AssociationEndMember( owner != PrincipalTable ? owner.Name : owner.Name + SelfRefSuffix, owner); _associationSet.TargetSet = _database.GetEntitySet(owner); if (!_database.AssociationTypes.Contains(_associationType)) { _database.AddAssociationType(_associationType); _database.AddAssociationSet(_associationSet); } } }
public void Apply(EdmModel model) { Check.NotNull(model, "model"); var associationPairs = (from a1 in model.AssociationTypes from a2 in model.AssociationTypes where a1 != a2 where a1.SourceEnd.GetEntityType() == a2.TargetEnd.GetEntityType() && a1.TargetEnd.GetEntityType() == a2.SourceEnd.GetEntityType() let a1Configuration = a1.GetConfiguration() as NavigationPropertyConfiguration let a2Configuration = a2.GetConfiguration() as NavigationPropertyConfiguration where (((a1Configuration == null) || ((a1Configuration.InverseEndKind == null) && (a1Configuration.InverseNavigationProperty == null))) && ((a2Configuration == null) || ((a2Configuration.InverseEndKind == null) && (a2Configuration.InverseNavigationProperty == null)))) select new { a1, a2 }) .Distinct((a, b) => a.a1 == b.a2 && a.a2 == b.a1) .GroupBy( (a, b) => a.a1.SourceEnd.GetEntityType() == b.a2.TargetEnd.GetEntityType() && a.a1.TargetEnd.GetEntityType() == b.a2.SourceEnd.GetEntityType()) .Where(g => g.Count() == 1) .Select(g => g.Single()); foreach (var pair in associationPairs) { var unifiedAssociation = pair.a2.GetConfiguration() != null ? pair.a2 : pair.a1; var redundantAssociation = unifiedAssociation == pair.a1 ? pair.a2 : pair.a1; unifiedAssociation.SourceEnd.RelationshipMultiplicity = redundantAssociation.TargetEnd.RelationshipMultiplicity; if (redundantAssociation.Constraint != null) { unifiedAssociation.Constraint = redundantAssociation.Constraint; unifiedAssociation.Constraint.FromRole = unifiedAssociation.SourceEnd; unifiedAssociation.Constraint.ToRole = unifiedAssociation.TargetEnd; } var sourceEndClrProperty = redundantAssociation.SourceEnd.GetClrPropertyInfo(); if (sourceEndClrProperty != null) { unifiedAssociation.TargetEnd.SetClrPropertyInfo(sourceEndClrProperty); } FixNavigationProperties(model, unifiedAssociation, redundantAssociation); model.RemoveAssociationType(redundantAssociation); } }
private void ConfigureInverse(AssociationType associationType, EdmModel model) { DebugCheck.NotNull(associationType); DebugCheck.NotNull(model); if (_inverseNavigationProperty == null) { return; } var inverseNavigationProperty = model.GetNavigationProperty(_inverseNavigationProperty); if ((inverseNavigationProperty != null) && (inverseNavigationProperty.Association != associationType)) { associationType.SourceEnd.RelationshipMultiplicity = inverseNavigationProperty.Association.TargetEnd.RelationshipMultiplicity; if ((associationType.Constraint == null) && (_constraint == null) && (inverseNavigationProperty.Association.Constraint != null)) { associationType.Constraint = inverseNavigationProperty.Association.Constraint; associationType.Constraint.DependentEnd = associationType.Constraint.DependentEnd.GetEntityType() == associationType.SourceEnd.GetEntityType() ? associationType.SourceEnd : associationType.TargetEnd; } model.RemoveAssociationType(inverseNavigationProperty.Association); inverseNavigationProperty.RelationshipType = associationType; inverseNavigationProperty.ToEndMember = associationType.SourceEnd; } }
public void RemoveAssociationType_should_remove_type_and_set() { var model = new EdmModel(DataSpace.CSpace); var sourceEntityType = new EntityType("E", "N", DataSpace.CSpace); var targetEntityType = new EntityType("E", "N", DataSpace.CSpace); model.AddEntitySet("S", sourceEntityType); model.AddEntitySet("T", targetEntityType); var associationType = model.AddAssociationType( "A", sourceEntityType, RelationshipMultiplicity.ZeroOrOne, targetEntityType, RelationshipMultiplicity.Many); model.AddAssociationSet("FooSet", associationType); model.RemoveAssociationType(associationType); Assert.Equal(0, model.AssociationTypes.Count()); Assert.Equal(0, model.Containers.First().AssociationSets.Count()); }
public void Apply(EdmModel model) { Check.NotNull(model, "model"); // Query the model for candidate complex types. // - The rules for complex type discovery are as follows: // 1) The entity does not have a key or base type. // 2) The entity does not have explicit configuration or has only structural type configuration. // 3) The entity does not have any outbound navigation properties. // The entity only has inbound associations where: // 4) The association does not have a constraint defined. // 5) The association does not have explicit configuration. // 6) The association is not self-referencing. // 7) The other end of the association is Optional. // 8) Any inbound navigation properties do not have explicit configuration. var candidates = from entityType in model.EntityTypes where entityType.KeyProperties.Count == 0 // (1) && entityType.BaseType == null // (1) let entityTypeConfiguration = entityType.GetConfiguration() as EntityTypeConfiguration where ((entityTypeConfiguration == null) // (2) || (!entityTypeConfiguration.IsExplicitEntity && entityTypeConfiguration.IsStructuralConfigurationOnly)) // (2) && !entityType.NavigationProperties.Any() // (3) let matchingAssociations = from associationType in model.AssociationTypes where associationType.SourceEnd.GetEntityType() == entityType || associationType.TargetEnd.GetEntityType() == entityType let declaringEnd = associationType.SourceEnd.GetEntityType() == entityType ? associationType.SourceEnd : associationType.TargetEnd let declaringEntity = associationType.GetOtherEnd(declaringEnd).GetEntityType() let navigationProperties = declaringEntity.NavigationProperties .Where(n => n.ResultEnd.GetEntityType() == entityType) select new { DeclaringEnd = declaringEnd, AssociationType = associationType, DeclaringEntityType = declaringEntity, NavigationProperties = navigationProperties.ToList() } where matchingAssociations.All( a => a.AssociationType.Constraint == null // (4) && a.AssociationType.GetConfiguration() == null // (5) && !a.AssociationType.IsSelfReferencing() // (6) && a.DeclaringEnd.IsOptional() // (7) && a.NavigationProperties.All(n => n.GetConfiguration() == null)) // (8) select new { EntityType = entityType, MatchingAssociations = matchingAssociations.ToList(), }; // Transform candidate entities into complex types foreach (var candidate in candidates.ToList()) { var complexType = model.AddComplexType(candidate.EntityType.Name, candidate.EntityType.NamespaceName); foreach (var property in candidate.EntityType.DeclaredProperties) { complexType.AddMember(property); } foreach (var annotation in candidate.EntityType.Annotations) { complexType.Annotations.Add(annotation); } foreach (var association in candidate.MatchingAssociations) { foreach (var navigationProperty in association.NavigationProperties) { if (association.DeclaringEntityType.NavigationProperties.Contains(navigationProperty)) { association.DeclaringEntityType.RemoveMember(navigationProperty); var complexProperty = association.DeclaringEntityType .AddComplexProperty(navigationProperty.Name, complexType); foreach (var annotation in navigationProperty.Annotations) { complexProperty.Annotations.Add(annotation); } } } model.RemoveAssociationType(association.AssociationType); } model.RemoveEntityType(candidate.EntityType); } }