private static IEnumerable<Tuple<ModificationFunctionMemberPath, EdmProperty>> GetIndependentFkColumns(
            EntityType entityType, DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(databaseMapping);

            foreach (var associationSetMapping in databaseMapping.GetAssociationSetMappings())
            {
                var associationType = associationSetMapping.AssociationSet.ElementType;

                if (associationType.IsManyToMany())
                {
                    continue;
                }

                AssociationEndMember _, dependentEnd;
                if (!associationType.TryGuessPrincipalAndDependentEnds(out _, out dependentEnd))
                {
                    dependentEnd = associationType.TargetEnd;
                }

                var dependentEntityType = dependentEnd.GetEntityType();

                if (dependentEntityType == entityType
                    || GetParents(entityType).Contains(dependentEntityType))
                {
                    var endPropertyMapping
                        = associationSetMapping.TargetEndMapping.AssociationEnd != dependentEnd
                              ? associationSetMapping.TargetEndMapping
                              : associationSetMapping.SourceEndMapping;

                    foreach (var propertyMapping in endPropertyMapping.PropertyMappings)
                    {
                        yield return
                            Tuple.Create(
                                new ModificationFunctionMemberPath(
                                    new EdmMember[] { propertyMapping.Property, dependentEnd },
                                    associationSetMapping.AssociationSet), propertyMapping.Column);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void RemoveRedundantTables(DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(databaseMapping);

            var tables
                = (from t in databaseMapping.Database.GetEntityTypes()
                   where databaseMapping.GetEntitySetMappings()
                                        .SelectMany(esm => esm.EntityTypeMappings)
                                        .SelectMany(etm => etm.MappingFragments).All(etmf => etmf.Table != t)
                         && databaseMapping.GetAssociationSetMappings().All(asm => asm.Table != t)
                   select t).ToList();

            tables.Each(
                t =>
                    {
                        var tableName = t.GetTableName();

                        if (tableName != null)
                        {
                            throw Error.OrphanedConfiguredTableDetected(tableName);
                        }

                        databaseMapping.Database.RemoveEntityType(t);
                    });
        }
Exemplo n.º 3
0
        public void GetEntitySetMappings_should_return_mappings()
        {
            var databaseMapping = new DbDatabaseMapping()
                .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));

            databaseMapping.AddAssociationSetMapping(
                new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), new EntitySet());

            Assert.Equal(1, databaseMapping.GetAssociationSetMappings().Count());
        }
        private void ConfigureAssociationMappings(
            DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(providerManifest);

            foreach (var configuration in _navigationPropertyConfigurations)
            {
                var propertyInfo = configuration.Key;
                var navigationPropertyConfiguration = configuration.Value;
                var navigationProperty = entityType.GetNavigationProperty(propertyInfo);

                if (navigationProperty == null)
                {
                    throw Error.NavigationPropertyNotFound(propertyInfo.Name, entityType.Name);
                }

                var associationSetMapping
                    = databaseMapping.GetAssociationSetMappings()
                        .SingleOrDefault(asm => asm.AssociationSet.ElementType == navigationProperty.Association);

                if (associationSetMapping != null)
                {
                    navigationPropertyConfiguration.Configure(associationSetMapping, databaseMapping, providerManifest);
                }
            }
        }
Exemplo n.º 5
0
        private static void UniquifyFunctionNames(DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(databaseMapping);

            foreach (var modificationStoredProcedureMapping
                in databaseMapping
                    .GetEntitySetMappings()
                    .SelectMany(esm => esm.ModificationFunctionMappings))
            {
                var entityTypeConfiguration
                    = (EntityTypeConfiguration)modificationStoredProcedureMapping.EntityType.GetConfiguration();

                if (entityTypeConfiguration.ModificationStoredProceduresConfiguration == null)
                {
                    continue;
                }

                var modificationStoredProceduresConfiguration
                    = entityTypeConfiguration.ModificationStoredProceduresConfiguration;

                UniquifyFunctionName(
                    databaseMapping,
                    modificationStoredProceduresConfiguration.InsertModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.InsertFunctionMapping);

                UniquifyFunctionName(
                    databaseMapping,
                    modificationStoredProceduresConfiguration.UpdateModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.UpdateFunctionMapping);

                UniquifyFunctionName(
                    databaseMapping,
                    modificationStoredProceduresConfiguration.DeleteModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.DeleteFunctionMapping);
            }

            foreach (var modificationStoredProcedureMapping
                in databaseMapping
                    .GetAssociationSetMappings()
                    .Select(asm => asm.ModificationFunctionMapping)
                    .Where(asm => asm != null))
            {
                var navigationPropertyConfiguration
                    = (NavigationPropertyConfiguration)modificationStoredProcedureMapping
                                                           .AssociationSet.ElementType.GetConfiguration();

                if (navigationPropertyConfiguration.ModificationStoredProceduresConfiguration == null)
                {
                    continue;
                }

                UniquifyFunctionName(
                    databaseMapping,
                    navigationPropertyConfiguration.ModificationStoredProceduresConfiguration.InsertModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.InsertFunctionMapping);

                UniquifyFunctionName(
                    databaseMapping,
                    navigationPropertyConfiguration.ModificationStoredProceduresConfiguration.DeleteModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.DeleteFunctionMapping);
            }
        }