private static void RemoveRedundantDefaultDiscriminators(TableMapping tableMapping)
 {
     foreach (var entitySet in tableMapping.EntityTypes.GetEntitySets())
     {
         (from cm in tableMapping.ColumnMappings
          from pm in cm.PropertyMappings
          where cm.PropertyMappings
          .Where(pm1 => tableMapping.EntityTypes.GetEntityTypes(entitySet).Contains(pm1.EntityType))
          .Count(pms => pms.IsDefaultDiscriminatorCondition) == 1
          select new
         {
             ColumnMapping = cm,
             PropertyMapping = pm
         }).ToArray().Each(
             x =>
         {
             x.PropertyMapping.Conditions.Clear();
             if (x.PropertyMapping.PropertyPath == null)
             {
                 x.ColumnMapping.PropertyMappings.Remove(x.PropertyMapping);
             }
         });
     }
 }
        /// <summary>
        ///     Determines if the table and entity type need mapping, and if not, removes the existing entity type mapping
        /// </summary>
        private bool FindPropertyEntityTypeMapping(
            TableMapping tableMapping,
            EntitySet entitySet,
            EntityType entityType,
            bool requiresIsTypeOf,
            out StorageEntityTypeMapping entityTypeMapping,
            out StorageMappingFragment fragment)
        {
            entityTypeMapping = null;
            fragment          = null;
            var mapping = (from etm in _databaseMapping.GetEntityTypeMappings(entityType)
                           from tmf in etm.MappingFragments
                           where tmf.Table == tableMapping.Table
                           select new
            {
                TypeMapping = etm,
                Fragment = tmf
            }).SingleOrDefault();

            if (mapping != null)
            {
                entityTypeMapping = mapping.TypeMapping;
                fragment          = mapping.Fragment;
                if (!requiresIsTypeOf &&
                    entityType.Abstract)
                {
                    RemoveFragment(entitySet, mapping.TypeMapping, mapping.Fragment);
                    return(false);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #3
0
 private static void RemoveRedundantDefaultDiscriminators(TableMapping tableMapping)
 {
     foreach (EntitySet entitySet1 in tableMapping.EntityTypes.GetEntitySets())
     {
         EntitySet entitySet = entitySet1;
         tableMapping.ColumnMappings.SelectMany((Func <ColumnMapping, IEnumerable <PropertyMappingSpecification> >)(cm => (IEnumerable <PropertyMappingSpecification>)cm.PropertyMappings), (cm, pm) => new
         {
             cm = cm,
             pm = pm
         }).Where(_param1 => _param1.cm.PropertyMappings.Where <PropertyMappingSpecification>((Func <PropertyMappingSpecification, bool>)(pm1 => tableMapping.EntityTypes.GetEntityTypes(entitySet).Contains <EntityType>(pm1.EntityType))).Count <PropertyMappingSpecification>((Func <PropertyMappingSpecification, bool>)(pms => pms.IsDefaultDiscriminatorCondition)) == 1).Select(_param0 => new
         {
             ColumnMapping   = _param0.cm,
             PropertyMapping = _param0.pm
         }).ToArray().Each(x =>
         {
             x.PropertyMapping.Conditions.Clear();
             if (x.PropertyMapping.PropertyPath != null)
             {
                 return;
             }
             x.ColumnMapping.PropertyMappings.Remove(x.PropertyMapping);
         });
     }
 }
예제 #4
0
 private bool DetermineRequiresIsTypeOf(
     TableMapping tableMapping,
     EntitySet entitySet,
     EntityType entityType)
 {
     if (!entityType.IsRootOfSet(tableMapping.EntityTypes.GetEntityTypes(entitySet)))
     {
         return(false);
     }
     if (tableMapping.EntityTypes.GetEntityTypes(entitySet).Count <EntityType>() <= 1 || !tableMapping.EntityTypes.GetEntityTypes(entitySet).Any <EntityType>((Func <EntityType, bool>)(et =>
     {
         if (et != entityType)
         {
             return(!et.Abstract);
         }
         return(false);
     })))
     {
         return(this._tableMappings.Values.Any <TableMapping>((Func <TableMapping, bool>)(tm =>
         {
             if (tm != tableMapping)
             {
                 return tm.Table.ForeignKeyBuilders.Any <ForeignKeyBuilder>((Func <ForeignKeyBuilder, bool>)(fk =>
                 {
                     if (fk.GetIsTypeConstraint())
                     {
                         return fk.PrincipalTable == tableMapping.Table;
                     }
                     return false;
                 }));
             }
             return false;
         })));
     }
     return(true);
 }
예제 #5
0
 private TableMapping FindOrCreateTableMapping(DbTableMetadata table)
 {
     TableMapping tableMapping;
     if (!_tableMappings.TryGetValue(table, out tableMapping))
     {
         tableMapping = new TableMapping(table);
         _tableMappings.Add(table, tableMapping);
     }
     return tableMapping;
 }
예제 #6
0
 private static bool HasConditions(TableMapping tableMapping, EdmEntityType entityType)
 {
     return tableMapping.ColumnMappings.SelectMany(cm => cm.PropertyMappings)
         .Any(pm => pm.EntityType == entityType && pm.Conditions.Count > 0);
 }
예제 #7
0
 private static void RemoveRedundantDefaultDiscriminators(TableMapping tableMapping)
 {
     foreach (var entitySet in tableMapping.EntityTypes.GetEntitySets())
     {
         (from cm in tableMapping.ColumnMappings
          from pm in cm.PropertyMappings
          where cm.PropertyMappings
                    .Where(pm1 => tableMapping.EntityTypes.GetEntityTypes(entitySet).Contains(pm1.EntityType))
                    .Count(pms => pms.IsDefaultDiscriminatorCondition) == 1
          select new
                     {
                         ColumnMapping = cm,
                         PropertyMapping = pm
                     }).ToArray().Each(
                         x =>
                             {
                                 x.PropertyMapping.Conditions.Clear();
                                 if (x.PropertyMapping.PropertyPath == null)
                                 {
                                     x.ColumnMapping.PropertyMappings.Remove(x.PropertyMapping);
                                 }
                             });
     }
 }
예제 #8
0
        /// <summary>
        ///     Determines if the table and entity type need mapping, and if not, removes the existing entity type mapping
        /// </summary>
        private bool FindPropertyEntityTypeMapping(
            TableMapping tableMapping,
            EdmEntitySet entitySet,
            EdmEntityType entityType,
            bool requiresIsTypeOf,
            out DbEntityTypeMapping entityTypeMapping,
            out DbEntityTypeMappingFragment fragment)
        {
            entityTypeMapping = null;
            fragment = null;
            var mapping = (from etm in _databaseMapping.GetEntityTypeMappings(entityType)
                           from tmf in etm.TypeMappingFragments
                           where tmf.Table == tableMapping.Table
                           select new
                                      {
                                          TypeMapping = etm,
                                          Fragment = tmf
                                      }).SingleOrDefault();

            if (mapping != null)
            {
                entityTypeMapping = mapping.TypeMapping;
                fragment = mapping.Fragment;
                if (!requiresIsTypeOf
                    && entityType.IsAbstract)
                {
                    RemoveFragment(entitySet, mapping.TypeMapping, mapping.Fragment);
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
예제 #9
0
 private static bool DetermineRequiresSplitEntityTypeMapping(
     TableMapping tableMapping,
     EdmEntityType entityType,
     bool requiresIsTypeOf)
 {
     return requiresIsTypeOf && HasConditions(tableMapping, entityType);
 }
예제 #10
0
 private bool DetermineRequiresIsTypeOf(
     TableMapping tableMapping, EdmEntitySet entitySet, EdmEntityType entityType)
 {
     // IsTypeOf if this is the root for this table and any derived type shares a property mapping
     return entityType.IsRootOfSet(tableMapping.EntityTypes.GetEntityTypes(entitySet)) &&
            ((tableMapping.EntityTypes.GetEntityTypes(entitySet).Count() > 1
              && tableMapping.EntityTypes.GetEntityTypes(entitySet).Any(et => et != entityType && !et.IsAbstract))
             ||
             _tableMappings.Values.Any(
                 tm =>
                 tm != tableMapping
                 &&
                 tm.Table.ForeignKeyConstraints.Any(
                     fk => fk.GetIsTypeConstraint() && fk.PrincipalTable == tableMapping.Table)));
 }
예제 #11
0
 private static DbEntityTypeMappingFragment FindConditionTypeMappingFragment(
     TableMapping tableMapping, DbEntityTypeMappingFragment propertiesTypeMappingFragment,
     DbEntityTypeMapping conditionTypeMapping)
 {
     var conditionTypeMappingFragment =
         conditionTypeMapping.TypeMappingFragments.SingleOrDefault(x => x.Table == tableMapping.Table);
     if (conditionTypeMappingFragment == null)
     {
         conditionTypeMappingFragment = EntityMappingOperations.CreateTypeMappingFragment(
             conditionTypeMapping, propertiesTypeMappingFragment, tableMapping.Table);
         conditionTypeMappingFragment.SetIsConditionOnlyFragment(true);
         if (propertiesTypeMappingFragment.GetDefaultDiscriminator() != null)
         {
             conditionTypeMappingFragment.SetDefaultDiscriminator(
                 propertiesTypeMappingFragment.GetDefaultDiscriminator());
             propertiesTypeMappingFragment.RemoveDefaultDiscriminatorAnnotation();
         }
     }
     return conditionTypeMappingFragment;
 }
예제 #12
0
        /// <summary>
        ///     Makes sure only the required property mappings are present
        /// </summary>
        private static void ConfigureTypeMappings(
            TableMapping tableMapping,
            Dictionary<EdmEntityType, DbEntityTypeMapping> rootMappings,
            EdmEntityType entityType,
            DbEntityTypeMappingFragment propertiesTypeMappingFragment,
            DbEntityTypeMappingFragment conditionTypeMappingFragment)
        {
            var existingPropertyMappings =
                new List<DbEdmPropertyMapping>(
                    propertiesTypeMappingFragment.PropertyMappings.Where(pm => !pm.Column.IsPrimaryKeyColumn));
            var existingConditions = new List<DbColumnCondition>(propertiesTypeMappingFragment.ColumnConditions);

            foreach (var columnMapping in from cm in tableMapping.ColumnMappings
                                          from pm in cm.PropertyMappings
                                          where pm.EntityType == entityType
                                          select new
                                                     {
                                                         cm.Column,
                                                         Property = pm
                                                     })
            {
                if (columnMapping.Property.PropertyPath != null
                    &&
                    !IsRootTypeMapping(
                        rootMappings, columnMapping.Property.EntityType, columnMapping.Property.PropertyPath))
                {
                    var existingPropertyMapping =
                        propertiesTypeMappingFragment.PropertyMappings.SingleOrDefault(
                            x => x.PropertyPath == columnMapping.Property.PropertyPath);
                    if (existingPropertyMapping != null)
                    {
                        existingPropertyMappings.Remove(existingPropertyMapping);
                    }
                    else
                    {
                        existingPropertyMapping = new DbEdmPropertyMapping
                                                      {
                                                          Column = columnMapping.Column,
                                                          PropertyPath = columnMapping.Property.PropertyPath
                                                      };
                        propertiesTypeMappingFragment.PropertyMappings.Add(existingPropertyMapping);
                    }
                }

                if (columnMapping.Property.Conditions != null)
                {
                    foreach (var condition in columnMapping.Property.Conditions)
                    {
                        if (conditionTypeMappingFragment.ColumnConditions.Contains(condition))
                        {
                            existingConditions.Remove(condition);
                        }
                        else if (!entityType.IsAbstract)
                        {
                            conditionTypeMappingFragment.ColumnConditions.Add(condition);
                        }
                    }
                }
            }

            // Any leftover mappings are removed
            foreach (var leftoverPropertyMapping in existingPropertyMappings)
            {
                propertiesTypeMappingFragment.PropertyMappings.Remove(leftoverPropertyMapping);
            }

            foreach (var leftoverCondition in existingConditions)
            {
                conditionTypeMappingFragment.ColumnConditions.Remove(leftoverCondition);
            }

            if (entityType.IsAbstract)
            {
                propertiesTypeMappingFragment.ColumnConditions.Clear();
            }
        }
 private static bool HasConditions(TableMapping tableMapping, EntityType entityType)
 {
     return(tableMapping.ColumnMappings.SelectMany(cm => cm.PropertyMappings)
            .Any(pm => pm.EntityType == entityType && pm.Conditions.Count > 0));
 }
        /// <summary>
        ///     Makes sure only the required property mappings are present
        /// </summary>
        private static void ConfigureTypeMappings(
            TableMapping tableMapping,
            Dictionary <EntityType, StorageEntityTypeMapping> rootMappings,
            EntityType entityType,
            StorageMappingFragment propertiesTypeMappingFragment,
            StorageMappingFragment conditionTypeMappingFragment)
        {
            var existingPropertyMappings =
                new List <ColumnMappingBuilder>(
                    propertiesTypeMappingFragment.ColumnMappings.Where(pm => !pm.ColumnProperty.IsPrimaryKeyColumn));
            var existingConditions = new List <StorageConditionPropertyMapping>(propertiesTypeMappingFragment.ColumnConditions);

            foreach (var columnMapping in from cm in tableMapping.ColumnMappings
                     from pm in cm.PropertyMappings
                     where pm.EntityType == entityType
                     select new
            {
                cm.Column,
                Property = pm
            })
            {
                if (columnMapping.Property.PropertyPath != null
                    &&
                    !IsRootTypeMapping(
                        rootMappings, columnMapping.Property.EntityType, columnMapping.Property.PropertyPath))
                {
                    var existingPropertyMapping =
                        propertiesTypeMappingFragment.ColumnMappings.SingleOrDefault(
                            x => x.PropertyPath == columnMapping.Property.PropertyPath);
                    if (existingPropertyMapping != null)
                    {
                        existingPropertyMappings.Remove(existingPropertyMapping);
                    }
                    else
                    {
                        existingPropertyMapping
                            = new ColumnMappingBuilder(columnMapping.Column, columnMapping.Property.PropertyPath);

                        propertiesTypeMappingFragment.AddColumnMapping(existingPropertyMapping);
                    }
                }

                if (columnMapping.Property.Conditions != null)
                {
                    foreach (var condition in columnMapping.Property.Conditions)
                    {
                        if (conditionTypeMappingFragment.ColumnConditions.Contains(condition))
                        {
                            existingConditions.Remove(condition);
                        }
                        else if (!entityType.Abstract)
                        {
                            conditionTypeMappingFragment.AddConditionProperty(condition);
                        }
                    }
                }
            }

            // Any leftover mappings are removed
            foreach (var leftoverPropertyMapping in existingPropertyMappings)
            {
                propertiesTypeMappingFragment.RemoveColumnMapping(leftoverPropertyMapping);
            }

            foreach (var leftoverCondition in existingConditions)
            {
                conditionTypeMappingFragment.RemoveConditionProperty(leftoverCondition);
            }

            if (entityType.Abstract)
            {
                propertiesTypeMappingFragment.ClearConditions();
            }
        }
예제 #15
0
        private static void ConfigureTypeMappings(
            TableMapping tableMapping,
            Dictionary <EntityType, EntityTypeMapping> rootMappings,
            EntityType entityType,
            MappingFragment propertiesTypeMappingFragment,
            MappingFragment conditionTypeMappingFragment)
        {
            List <ColumnMappingBuilder>     columnMappingBuilderList     = new List <ColumnMappingBuilder>(propertiesTypeMappingFragment.ColumnMappings.Where <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => !pm.ColumnProperty.IsPrimaryKeyColumn)));
            List <ConditionPropertyMapping> conditionPropertyMappingList = new List <ConditionPropertyMapping>(propertiesTypeMappingFragment.ColumnConditions);

            foreach (var data in tableMapping.ColumnMappings.SelectMany((Func <ColumnMapping, IEnumerable <PropertyMappingSpecification> >)(cm => (IEnumerable <PropertyMappingSpecification>)cm.PropertyMappings), (cm, pm) => new
            {
                cm = cm,
                pm = pm
            }).Where(_param1 => _param1.pm.EntityType == entityType).Select(_param0 => new
            {
                Column = _param0.cm.Column,
                Property = _param0.pm
            }))
            {
                var columnMapping = data;
                if (columnMapping.Property.PropertyPath != null && !EntityMappingService.IsRootTypeMapping(rootMappings, columnMapping.Property.EntityType, columnMapping.Property.PropertyPath))
                {
                    ColumnMappingBuilder columnMappingBuilder1 = propertiesTypeMappingFragment.ColumnMappings.SingleOrDefault <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(x => x.PropertyPath == columnMapping.Property.PropertyPath));
                    if (columnMappingBuilder1 != null)
                    {
                        columnMappingBuilderList.Remove(columnMappingBuilder1);
                    }
                    else
                    {
                        ColumnMappingBuilder columnMappingBuilder2 = new ColumnMappingBuilder(columnMapping.Column, columnMapping.Property.PropertyPath);
                        propertiesTypeMappingFragment.AddColumnMapping(columnMappingBuilder2);
                    }
                }
                if (columnMapping.Property.Conditions != null)
                {
                    foreach (ConditionPropertyMapping condition in (IEnumerable <ConditionPropertyMapping>)columnMapping.Property.Conditions)
                    {
                        if (conditionTypeMappingFragment.ColumnConditions.Contains <ConditionPropertyMapping>(condition))
                        {
                            conditionPropertyMappingList.Remove(condition);
                        }
                        else if (!entityType.Abstract)
                        {
                            conditionTypeMappingFragment.AddConditionProperty(condition);
                        }
                    }
                }
            }
            foreach (ColumnMappingBuilder columnMappingBuilder in columnMappingBuilderList)
            {
                propertiesTypeMappingFragment.RemoveColumnMapping(columnMappingBuilder);
            }
            foreach (ConditionPropertyMapping condition in conditionPropertyMappingList)
            {
                conditionTypeMappingFragment.RemoveConditionProperty(condition);
            }
            if (!entityType.Abstract)
            {
                return;
            }
            propertiesTypeMappingFragment.ClearConditions();
        }