Пример #1
0
        private void ConfigureDependentBehavior(
            AssociationType associationType, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
        {
            DebugCheck.NotNull(associationType);
            DebugCheck.NotNull(model);
            DebugCheck.NotNull(entityTypeConfiguration);

            AssociationEndMember principalEnd;
            AssociationEndMember dependentEnd;

            if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                if (IsNavigationPropertyDeclaringTypePrincipal.HasValue)
                {
                    associationType.MarkPrincipalConfigured();

                    var navProp = model.EntityTypes
                                  .SelectMany(et => et.DeclaredNavigationProperties)
                                  .Single(
                        np => np.RelationshipType.Equals(associationType) &&  // CodePlex 546
                        np.GetClrPropertyInfo().IsSameAs(NavigationProperty));

                    principalEnd = IsNavigationPropertyDeclaringTypePrincipal.Value
                                       ? associationType.GetOtherEnd(navProp.ResultEnd)
                                       : navProp.ResultEnd;

                    dependentEnd = associationType.GetOtherEnd(principalEnd);

                    if (associationType.SourceEnd != principalEnd)
                    {
                        // need to move around source to be principal, target to be dependent so Edm services will use the correct
                        // principal and dependent ends. The Edm default Db + mapping service tries to guess principal/dependent
                        // based on multiplicities, but if it can't figure it out, it will use source as principal and target as dependent
                        associationType.SourceEnd = principalEnd;
                        associationType.TargetEnd = dependentEnd;

                        var associationSet
                            = model.Containers
                              .SelectMany(ct => ct.AssociationSets)
                              .Single(aset => aset.ElementType == associationType);

                        var sourceSet = associationSet.SourceSet;

                        associationSet.SourceSet = associationSet.TargetSet;
                        associationSet.TargetSet = sourceSet;
                    }
                }

                if (principalEnd == null)
                {
                    dependentEnd = associationType.TargetEnd;
                }
            }

            ConfigureConstraint(associationType, dependentEnd, entityTypeConfiguration);
            ConfigureDeleteAction(associationType.GetOtherEnd(dependentEnd));
        }
Пример #2
0
        private void ConfigureDependentBehavior(
            AssociationType associationType,
            EdmModel model,
            EntityTypeConfiguration entityTypeConfiguration)
        {
            AssociationEndMember principalEnd;
            AssociationEndMember dependentEnd;

            if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                if (this.IsNavigationPropertyDeclaringTypePrincipal.HasValue)
                {
                    associationType.MarkPrincipalConfigured();
                    System.Data.Entity.Core.Metadata.Edm.NavigationProperty navigationProperty = model.EntityTypes.SelectMany <EntityType, System.Data.Entity.Core.Metadata.Edm.NavigationProperty>((Func <EntityType, IEnumerable <System.Data.Entity.Core.Metadata.Edm.NavigationProperty> >)(et => (IEnumerable <System.Data.Entity.Core.Metadata.Edm.NavigationProperty>)et.DeclaredNavigationProperties)).Single <System.Data.Entity.Core.Metadata.Edm.NavigationProperty>((Func <System.Data.Entity.Core.Metadata.Edm.NavigationProperty, bool>)(np =>
                    {
                        if (np.RelationshipType.Equals((object)associationType))
                        {
                            return(np.GetClrPropertyInfo().IsSameAs(this.NavigationProperty));
                        }
                        return(false);
                    }));
                    principalEnd = this.IsNavigationPropertyDeclaringTypePrincipal.Value ? associationType.GetOtherEnd(navigationProperty.ResultEnd) : navigationProperty.ResultEnd;
                    dependentEnd = associationType.GetOtherEnd(principalEnd);
                    if (associationType.SourceEnd != principalEnd)
                    {
                        associationType.SourceEnd = principalEnd;
                        associationType.TargetEnd = dependentEnd;
                        AssociationSet associationSet = model.Containers.SelectMany <EntityContainer, AssociationSet>((Func <EntityContainer, IEnumerable <AssociationSet> >)(ct => (IEnumerable <AssociationSet>)ct.AssociationSets)).Single <AssociationSet>((Func <AssociationSet, bool>)(aset => aset.ElementType == associationType));
                        EntitySet      sourceSet      = associationSet.SourceSet;
                        associationSet.SourceSet = associationSet.TargetSet;
                        associationSet.TargetSet = sourceSet;
                    }
                }
                if (principalEnd == null)
                {
                    dependentEnd = associationType.TargetEnd;
                }
            }
            this.ConfigureConstraint(associationType, dependentEnd, entityTypeConfiguration);
            this.ConfigureDeleteAction(associationType.GetOtherEnd(dependentEnd));
        }
        public void Apply_should_introduce_constraint_when_one_to_one()
        {
            var entityType1 = new EntityType();

            entityType1.AddKeyMember(EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            var entityType2 = new EntityType();

            entityType2.AddKeyMember(EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            var associationType = new AssociationType();

            associationType.SourceEnd = new AssociationEndMember("S", entityType2);
            associationType.TargetEnd = new AssociationEndMember("T", entityType1);

            associationType.MarkPrincipalConfigured();

            ((IEdmConvention <AssociationType>) new OneToOneConstraintIntroductionConvention())
            .Apply(associationType, new EdmModel());

            Assert.NotNull(associationType.Constraint);
            Assert.Equal(1, associationType.Constraint.ToProperties.Count);
        }
        public void Apply_should_introduce_constraint_when_one_to_one()
        {
            var entityType1 = new EntityType("E", "N", DataSpace.CSpace);

            entityType1.AddKeyMember(EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            var entityType2 = new EntityType("E", "N", DataSpace.CSpace);

            entityType2.AddKeyMember(EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            associationType.SourceEnd = new AssociationEndMember("S", entityType2);
            associationType.TargetEnd = new AssociationEndMember("T", entityType1);

            associationType.MarkPrincipalConfigured();

            (new OneToOneConstraintIntroductionConvention())
            .Apply(associationType, new DbModel(new EdmModel(DataSpace.CSpace), null));

            Assert.NotNull(associationType.Constraint);
            Assert.Equal(1, associationType.Constraint.ToProperties.Count);
        }