Ejemplo n.º 1
0
        private void GenerateIndependentAssociationType(
            AssociationType associationType,
            DbDatabaseMapping databaseMapping)
        {
            AssociationEndMember principalEnd;
            AssociationEndMember dependentEnd;

            if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                if (!associationType.IsPrincipalConfigured())
                {
                    throw Error.UnableToDeterminePrincipal((object)EntityTypeExtensions.GetClrType(associationType.SourceEnd.GetEntityType()), (object)EntityTypeExtensions.GetClrType(associationType.TargetEnd.GetEntityType()));
                }
                principalEnd = associationType.SourceEnd;
                dependentEnd = associationType.TargetEnd;
            }
            EntityTypeMapping     mappingInHierarchy = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType());
            EntityType            table = mappingInHierarchy.MappingFragments.First <MappingFragment>().Table;
            AssociationSetMapping associationSetMapping = AssociationTypeMappingGenerator.GenerateAssociationSetMapping(associationType, databaseMapping, principalEnd, dependentEnd, table);

            this.GenerateIndependentForeignKeyConstraint(databaseMapping, principalEnd.GetEntityType(), dependentEnd.GetEntityType(), table, associationSetMapping, associationSetMapping.SourceEndMapping, associationType.Name, principalEnd, false);
            foreach (EdmProperty keyProperty in dependentEnd.GetEntityType().KeyProperties())
            {
                associationSetMapping.TargetEndMapping.AddPropertyMapping(new ScalarPropertyMapping(keyProperty, mappingInHierarchy.GetPropertyMapping(keyProperty).ColumnProperty));
            }
        }
Ejemplo n.º 2
0
 private static IEnumerable <Tuple <ModificationFunctionMemberPath, EdmProperty> > GetIndependentFkColumns(
     EntityType entityType,
     DbDatabaseMapping databaseMapping)
 {
     foreach (AssociationSetMapping associationSetMapping in databaseMapping.GetAssociationSetMappings())
     {
         AssociationType associationType = associationSetMapping.AssociationSet.ElementType;
         if (!associationType.IsManyToMany())
         {
             AssociationEndMember _;
             AssociationEndMember dependentEnd;
             if (!associationType.TryGuessPrincipalAndDependentEnds(out _, out dependentEnd))
             {
                 dependentEnd = associationType.TargetEnd;
             }
             EntityType dependentEntityType = dependentEnd.GetEntityType();
             if (dependentEntityType == entityType || ModificationFunctionMappingGenerator.GetParents(entityType).Contains <EntityType>(dependentEntityType))
             {
                 EndPropertyMapping endPropertyMapping = associationSetMapping.TargetEndMapping.AssociationEnd != dependentEnd ? associationSetMapping.TargetEndMapping : associationSetMapping.SourceEndMapping;
                 foreach (ScalarPropertyMapping propertyMapping in endPropertyMapping.PropertyMappings)
                 {
                     yield return(Tuple.Create <ModificationFunctionMemberPath, EdmProperty>(new ModificationFunctionMemberPath((IEnumerable <EdmMember>) new EdmMember[2]
                     {
                         (EdmMember)propertyMapping.Property,
                         (EdmMember)dependentEnd
                     }, associationSetMapping.AssociationSet), propertyMapping.Column));
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
        private static void UpdatePrincipalTables(
            DbDatabaseMapping databaseMapping,
            EntityType toTable,
            bool removeFks,
            AssociationType associationType,
            EntityType et)
        {
            List <AssociationEndMember> associationEndMemberList = new List <AssociationEndMember>();
            AssociationEndMember        principalEnd;
            AssociationEndMember        dependentEnd;

            if (associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                associationEndMemberList.Add(principalEnd);
            }
            else if (associationType.SourceEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many && associationType.TargetEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
                associationEndMemberList.Add(associationType.SourceEnd);
                associationEndMemberList.Add(associationType.TargetEnd);
            }
            else
            {
                associationEndMemberList.Add(associationType.SourceEnd);
            }
            foreach (AssociationEndMember associationEnd in associationEndMemberList)
            {
                if (associationEnd.GetEntityType() == et)
                {
                    IEnumerable <KeyValuePair <EntityType, IEnumerable <EdmProperty> > > keyValuePairs;
                    if (associationType.Constraint != null)
                    {
                        EntityType entityType = associationType.GetOtherEnd(associationEnd).GetEntityType();
                        keyValuePairs = databaseMapping.Model.GetSelfAndAllDerivedTypes(entityType).Select <EntityType, EntityTypeMapping>((Func <EntityType, EntityTypeMapping>)(t => databaseMapping.GetEntityTypeMapping(t))).Where <EntityTypeMapping>((Func <EntityTypeMapping, bool>)(dm => dm != null)).SelectMany <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(dm => dm.MappingFragments.Where <MappingFragment>((Func <MappingFragment, bool>)(tmf => associationType.Constraint.ToProperties.All <EdmProperty>((Func <EdmProperty, bool>)(p => tmf.ColumnMappings.Any <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.PropertyPath.First <EdmProperty>() == p)))))))).Distinct <MappingFragment>((Func <MappingFragment, MappingFragment, bool>)((f1, f2) => f1.Table == f2.Table)).Select <MappingFragment, KeyValuePair <EntityType, IEnumerable <EdmProperty> > >((Func <MappingFragment, KeyValuePair <EntityType, IEnumerable <EdmProperty> > >)(df => new KeyValuePair <EntityType, IEnumerable <EdmProperty> >(df.Table, df.ColumnMappings.Where <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => associationType.Constraint.ToProperties.Contains(pm.PropertyPath.First <EdmProperty>()))).Select <ColumnMappingBuilder, EdmProperty>((Func <ColumnMappingBuilder, EdmProperty>)(pm => pm.ColumnProperty)))));
                    }
                    else
                    {
                        AssociationSetMapping associationSetMapping = databaseMapping.EntityContainerMappings.Single <EntityContainerMapping>().AssociationSetMappings.Single <AssociationSetMapping>((Func <AssociationSetMapping, bool>)(asm => asm.AssociationSet.ElementType == associationType));
                        keyValuePairs = (IEnumerable <KeyValuePair <EntityType, IEnumerable <EdmProperty> > >) new KeyValuePair <EntityType, IEnumerable <EdmProperty> >[1]
                        {
                            new KeyValuePair <EntityType, IEnumerable <EdmProperty> >(associationSetMapping.Table, (associationSetMapping.SourceEndMapping.AssociationEnd == associationEnd ? (IEnumerable <ScalarPropertyMapping>)associationSetMapping.SourceEndMapping.PropertyMappings : (IEnumerable <ScalarPropertyMapping>)associationSetMapping.TargetEndMapping.PropertyMappings).Select <ScalarPropertyMapping, EdmProperty>((Func <ScalarPropertyMapping, EdmProperty>)(pm => pm.Column)))
                        };
                    }
                    foreach (KeyValuePair <EntityType, IEnumerable <EdmProperty> > keyValuePair in keyValuePairs)
                    {
                        KeyValuePair <EntityType, IEnumerable <EdmProperty> > tableInfo = keyValuePair;
                        foreach (ForeignKeyBuilder foreignKeyBuilder in tableInfo.Key.ForeignKeyBuilders.Where <ForeignKeyBuilder>((Func <ForeignKeyBuilder, bool>)(fk => fk.DependentColumns.SequenceEqual <EdmProperty>(tableInfo.Value))).ToArray <ForeignKeyBuilder>())
                        {
                            if (removeFks)
                            {
                                tableInfo.Key.RemoveForeignKey(foreignKeyBuilder);
                            }
                            else if (foreignKeyBuilder.GetAssociationType() == null || foreignKeyBuilder.GetAssociationType() == associationType)
                            {
                                foreignKeyBuilder.PrincipalTable = toTable;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
0
        private void GenerateIndependentAssociationType(
            AssociationType associationType, DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(associationType);
            DebugCheck.NotNull(databaseMapping);

            AssociationEndMember principalEnd, dependentEnd;

            if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                if (!associationType.IsPrincipalConfigured())
                {
                    throw Error.UnableToDeterminePrincipal(
                              associationType.SourceEnd.GetEntityType().GetClrType(),
                              associationType.TargetEnd.GetEntityType().GetClrType());
                }

                principalEnd = associationType.SourceEnd;
                dependentEnd = associationType.TargetEnd;
            }

            var dependentEntityTypeMapping = GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType());

            var dependentTable = dependentEntityTypeMapping
                                 .MappingFragments
                                 .First()
                                 .Table;

            var associationSetMapping
                = GenerateAssociationSetMapping(
                      associationType, databaseMapping, principalEnd, dependentEnd, dependentTable);

            GenerateIndependentForeignKeyConstraint(
                databaseMapping,
                principalEnd.GetEntityType(),
                dependentEnd.GetEntityType(),
                dependentTable,
                associationSetMapping,
                associationSetMapping.SourceEndMapping,
                associationType.Name,
                principalEnd);

            foreach (var property in dependentEnd.GetEntityType().KeyProperties())
            {
                associationSetMapping.TargetEndMapping
                .AddProperty(
                    new StorageScalarPropertyMapping(
                        property,
                        dependentEntityTypeMapping.GetPropertyMapping(property).ColumnProperty));
            }
        }
        public void TryGuessPrincipalAndDependentEnds_should_return_correct_ends_for_optional_to_required()
        {
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
            associationType.SourceEnd.RelationshipMultiplicity = RelationshipMultiplicity.ZeroOrOne;
            associationType.TargetEnd.RelationshipMultiplicity = RelationshipMultiplicity.One;

            AssociationEndMember principalEnd, dependentEnd;

            associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd);

            Assert.Same(associationType.TargetEnd, principalEnd);
            Assert.Same(associationType.SourceEnd, dependentEnd);
        }
        public void TryGuessPrincipalAndDependentEnds_should_return_correct_ends_for_optional_to_required()
        {
            var associationType = new AssociationType();

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType());
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType());
            associationType.SourceEnd.RelationshipMultiplicity = RelationshipMultiplicity.ZeroOrOne;
            associationType.TargetEnd.RelationshipMultiplicity = RelationshipMultiplicity.One;

            AssociationEndMember principalEnd, dependentEnd;

            associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd);

            Assert.Same(associationType.TargetEnd, principalEnd);
            Assert.Same(associationType.SourceEnd, dependentEnd);
        }
Ejemplo n.º 8
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));
        }
Ejemplo n.º 9
0
        /// <inheritdoc />
        public virtual void Apply(NavigationProperty item, DbModel model)
        {
            Check.NotNull <NavigationProperty>(item, nameof(item));
            Check.NotNull <DbModel>(model, nameof(model));
            AssociationType association = item.Association;

            if (association.Constraint != null)
            {
                return;
            }
            ForeignKeyAttribute  foreignKeyAttribute = item.GetClrAttributes <ForeignKeyAttribute>().SingleOrDefault <ForeignKeyAttribute>();
            AssociationEndMember principalEnd;
            AssociationEndMember dependentEnd;

            if (foreignKeyAttribute == null || !association.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd) && !association.IsPrincipalConfigured())
            {
                return;
            }
            AssociationEndMember associationEndMember = dependentEnd ?? association.TargetEnd;

            principalEnd = principalEnd ?? association.SourceEnd;
            IEnumerable <string>      dependentPropertyNames = ((IEnumerable <string>)foreignKeyAttribute.Name.Split(',')).Select <string, string>((Func <string, string>)(p => p.Trim()));
            EntityType                declaringEntityType    = model.ConceptualModel.EntityTypes.Single <EntityType>((Func <EntityType, bool>)(e => e.DeclaredNavigationProperties.Contains(item)));
            List <EdmProperty>        list       = ForeignKeyNavigationPropertyAttributeConvention.GetDependentProperties(associationEndMember.GetEntityType(), dependentPropertyNames, declaringEntityType, item).ToList <EdmProperty>();
            ReferentialConstraint     constraint = new ReferentialConstraint((RelationshipEndMember)principalEnd, (RelationshipEndMember)associationEndMember, (IEnumerable <EdmProperty>)principalEnd.GetEntityType().KeyProperties().ToList <EdmProperty>(), (IEnumerable <EdmProperty>)list);
            IEnumerable <EdmProperty> source     = associationEndMember.GetEntityType().KeyProperties();

            if (source.Count <EdmProperty>() == constraint.ToProperties.Count <EdmProperty>() && source.All <EdmProperty>((Func <EdmProperty, bool>)(kp => constraint.ToProperties.Contains(kp))))
            {
                principalEnd.RelationshipMultiplicity = RelationshipMultiplicity.One;
                if (associationEndMember.RelationshipMultiplicity.IsMany())
                {
                    associationEndMember.RelationshipMultiplicity = RelationshipMultiplicity.ZeroOrOne;
                }
            }
            if (principalEnd.IsRequired())
            {
                constraint.ToProperties.Each <EdmProperty, bool>((Func <EdmProperty, bool>)(p => p.Nullable = false));
            }
            association.Constraint = constraint;
        }
        private static void UpdatePrincipalTables(
            DbDatabaseMapping databaseMapping, EntityType toTable, bool removeFks,
            AssociationType associationType, EntityType et)
        {
            AssociationEndMember principalEnd, dependentEnd;
            var endsToCheck = new List <AssociationEndMember>();

            if (associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                endsToCheck.Add(principalEnd);
            }
            else if (associationType.SourceEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many &&
                     associationType.TargetEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
                // many to many consider both ends
                endsToCheck.Add(associationType.SourceEnd);
                endsToCheck.Add(associationType.TargetEnd);
            }
            else
            {
                // 1:1 and 0..1:0..1
                endsToCheck.Add(associationType.SourceEnd);
            }

            foreach (var end in endsToCheck)
            {
                if (end.GetEntityType() == et)
                {
                    IEnumerable <KeyValuePair <EntityType, IEnumerable <EdmProperty> > > dependentTableInfos;
                    if (associationType.Constraint != null)
                    {
                        var originalDependentType = associationType.GetOtherEnd(end).GetEntityType();
                        var allDependentTypes     = databaseMapping.Model.GetSelfAndAllDerivedTypes(originalDependentType);

                        dependentTableInfos =
                            allDependentTypes.Select(t => databaseMapping.GetEntityTypeMapping(t)).Where(
                                dm => dm != null)
                            .SelectMany(
                                dm => dm.MappingFragments
                                .Where(
                                    tmf => associationType.Constraint.ToProperties
                                    .All(
                                        p =>
                                        tmf.ColumnMappings.Any(
                                            pm => pm.PropertyPath.First() == p))))
                            .Distinct((f1, f2) => f1.Table == f2.Table)
                            .Select(
                                df =>
                                new KeyValuePair <EntityType, IEnumerable <EdmProperty> >(
                                    df.Table,
                                    df.ColumnMappings.Where(
                                        pm =>
                                        associationType.Constraint.ToProperties.Contains(
                                            pm.PropertyPath.First())).Select(
                                        pm => pm.ColumnProperty)));
                    }
                    else
                    {
                        // IA
                        var associationSetMapping =
                            databaseMapping.EntityContainerMappings
                            .Single().AssociationSetMappings
                            .Single(asm => asm.AssociationSet.ElementType == associationType);

                        var dependentTable   = associationSetMapping.Table;
                        var propertyMappings = associationSetMapping.SourceEndMapping.EndMember == end
                                                   ? associationSetMapping.SourceEndMapping.PropertyMappings
                                                   : associationSetMapping.TargetEndMapping.PropertyMappings;
                        var dependentColumns = propertyMappings.Select(pm => pm.ColumnProperty);

                        dependentTableInfos = new[]
                        {
                            new KeyValuePair
                            <EntityType, IEnumerable <EdmProperty> >(
                                dependentTable, dependentColumns)
                        };
                    }

                    foreach (var tableInfo in dependentTableInfos)
                    {
                        foreach (
                            var fk in
                            tableInfo.Key.ForeignKeyBuilders.Where(
                                fk => fk.DependentColumns.SequenceEqual(tableInfo.Value)).ToArray(

                                ))
                        {
                            if (removeFks)
                            {
                                tableInfo.Key.RemoveForeignKey(fk);
                            }
                            else
                            {
                                fk.PrincipalTable = toTable;
                            }
                        }
                    }
                }
            }
        }