public void Apply(EdmModel model) { Check.NotNull(model, "model"); var associationPairs = (from a1 in model.GetAssociationTypes() from a2 in model.GetAssociationTypes() 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.DependentEnd = unifiedAssociation.Constraint.DependentEnd.GetEntityType() == unifiedAssociation.SourceEnd.GetEntityType() ? unifiedAssociation.SourceEnd : unifiedAssociation.TargetEnd; } FixNavigationProperties(model, unifiedAssociation, redundantAssociation); model.RemoveAssociationType(redundantAssociation); } }
public virtual void SetOwner(EntityType owner) { Util.ThrowIfReadOnly(this); if (owner == null) { _database.RemoveAssociationType(_assocationType); } else { _assocationType.TargetEnd = new AssociationEndMember( owner != PrincipalTable ? owner.Name : owner.Name + SelfRefSuffix, owner); if (!_database.GetAssociationTypes().Contains(_assocationType)) { _database.AddAssociationType(_assocationType); _database.AddAssociationSet(_associationSet); } } }
public void Configure_should_configure_inverse() { var inverseMockPropertyInfo = new MockPropertyInfo(); var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo()) { InverseNavigationProperty = inverseMockPropertyInfo }; var associationType = new AssociationType(); associationType.SourceEnd = new AssociationEndMember("S", new EntityType()); associationType.TargetEnd = new AssociationEndMember("T", new EntityType()); var inverseAssociationType = new AssociationType(); inverseAssociationType.SourceEnd = new AssociationEndMember("S", new EntityType()); inverseAssociationType.TargetEnd = new AssociationEndMember("T", new EntityType()); var model = new EdmModel().Initialize(); model.AddAssociationType(inverseAssociationType); var inverseNavigationProperty = model.AddEntityType("T") .AddNavigationProperty("N", inverseAssociationType); inverseNavigationProperty.SetClrPropertyInfo(inverseMockPropertyInfo); navigationPropertyConfiguration.Configure( new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType())) { RelationshipType = associationType }, model, new EntityTypeConfiguration(typeof(object))); Assert.Same(associationType, inverseNavigationProperty.Association); Assert.Same(associationType.SourceEnd, inverseNavigationProperty.ResultEnd); Assert.Equal(0, model.GetAssociationTypes().Count()); }